当前位置:   article > 正文

Python数据分析-4_plt.bar(range(len(_x)), _y)

plt.bar(range(len(_x)), _y)

1.对于一组电影数据,呈现出rating,runtime的分布情况:

  1. #encoding=utf-8
  2. import pandas as pd
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. file_path = "./youtube_video_data/IMDB-Movie-Data.csv"
  6. df = pd.read_csv(file_path)
  7. #print(df.head(1))#读取第一行
  8. #print(df.info())#读取Data columns,显示数据条数
  9. #rating,runtime分布情况
  10. #选择图形,直方图
  11. #准备数据
  12. runtime_data = df["Runtime (Minutes)"].values
  13. #print(runtime_data)#读取运行时间的分钟数
  14. max_runtime = runtime_data.max()
  15. min_runtime = runtime_data.min()
  16. num_bin = (max_runtime - min_runtime)//10#显示直方图的组数
  17. #设置图形的大小
  18. plt.figure(figsize=(20,8),dpi=80)
  19. plt.hist(runtime_data,num_bin)#显示直方图
  20. plt.xticks(range(min_runtime,max_runtime+5,5))
  21. plt.show()
  22. #rating的显示类比以上代码

2.统计电影分类(genre)的情况(重新构造一个全为0的数组,列名为分类,如果一条数据中分类出现过,就让0变为1):

  1. #encoding=utf-8
  2. import pandas as pd
  3. import numpy as np
  4. from matplotlib import pyplot as plt
  5. file_path = "./youtube_video_data/IMDB-Movie-Data.csv"
  6. df = pd.read_csv(file_path)
  7. #print(df.head(1))
  8. #print(df["Genre"])#输出Genre的数据
  9. #统计分类的列表
  10. temp_list = df["Genre"].str.split(",").tolist()#[[],[],[]...]
  11. #print(temp_list)
  12. genre_list = list(set([i for j in temp_list for i in j]))
  13. #print(genre_list)
  14. #构造全为0的数组
  15. zeros_df = pd.DataFrame(np.zeros((df.shape[0],len(genre_list))),columns = genre_list)
  16. #print(df.shape[0])#输出的结果为行数1000
  17. #print(zeros_df)
  18. #给每个电影出现分类的位置赋值1
  19. for i in range(df.shape[0]):#遍历每一行
  20. #zeros_df.loc[0,["Sci-fi","Mucical"]] = 1
  21. zeros_df.loc[i,temp_list[i]] = 1 #把第i行,第temp_list[i]列的数设置为1
  22. #print(zeros_df.head(3))
  23. #统计每个分类的电影的数量和
  24. genre_count = zeros_df.sum(axis=0)
  25. #print(genre_count)
  26. #排序
  27. genre_count = genre_count.sort_values()
  28. _x = genre_count.index
  29. _y = genre_count.values
  30. #print(_x)
  31. #print(_y)
  32. #画图
  33. plt.figure(figsize=(20,8),dpi=80)
  34. plt.bar(range(len(_x)),_y)
  35. plt.xticks(range(len(_x)),_x)
  36. plt.show()

3.数据合并

join : 默认情况下它是把行索引相同的数据合并到一起

merge :按照指定的列把数据按照一定的方式合并到一起

4.全球星巴克店铺的统计数据,美国的星巴克数量和中国的哪个多,中国每个省份星巴克的数量:

  1. #encoding=utf-8
  2. import pandas as pd
  3. import numpy as np
  4. file_path = './youtube_video_data/starbucks_store_worldwide.csv'
  5. read_data = pd.read_csv(file_path)
  6. #print(read_data)
  7. #print(read_data.head(1))
  8. #print(read_data.info())
  9. grouped = read_data.groupby(by="Country")
  10. print(grouped)
  11. #DataFrameGroupBy
  12. #可以进行遍历
  13. # for i,j in grouped:
  14. # print(i)
  15. # print("-"*100)
  16. # print(j,type(j))
  17. # print("*"*100)
  18. #read_data[read_data["Country"]=="US"]
  19. #调用聚合方法,显示中国和美国的店铺数量
  20. #print(grouped["Brand"].count())
  21. # country_count = grouped["Brand"].count()
  22. # print(country_count["US"])
  23. # print(country_count["CN"])
  24. #统计中国每个省店铺的数量
  25. china_data = read_data[read_data["Country"] == "CN"]
  26. #print(china_data)
  27. grouped = china_data.groupby(by="State/Province").count()["Brand"]
  28. #print(grouped)
  29. df = read_data
  30. #数据按照多个条件进行分组
  31. grouped = df["Brand"].groupby(by=[(df["Country"]),df["State/Province"]]).count()
  32. # print(grouped)
  33. # print(type(grouped))
  34. #数据按照多个条件进行分组,返回DataFrame
  35. grouped1 = df["Brand"].groupby(by=[(df["Country"]),df["State/Province"]]).count()
  36. grouped2 = df.groupby(by=[df["Country"],df["State/Province"]])[["Brand"]].count()
  37. grouped3 = df.groupby(by=[df["Country"],df["State/Province"]]).count()[["Brand"]]
  38. # print(grouped1,type(grouped1))
  39. # print(grouped2,type(grouped2))
  40. # print(grouped3,type(grouped3))
  41. print(grouped1.index)

5.分组和聚合:

  1. # coding=utf-8
  2. import pandas as pd
  3. from matplotlib import pyplot as plt
  4. from matplotlib import font_manager
  5. my_font = font_manager.FontProperties(fname=r"c:\windows\fonts\simsun.ttc")
  6. file_path = "./youtube_video_data/starbucks_store_worldwide.csv"
  7. df = pd.read_csv(file_path)
  8. df = df[df["Country"]=="CN"]
  9. #使用matplotlib呈现出店铺总数排名前10的国家
  10. #准备数据
  11. data1 = df.groupby(by="City").count()["Brand"].sort_values(ascending=False)[:25]
  12. _x = data1.index
  13. _y = data1.values
  14. #画图
  15. plt.figure(figsize=(20,12),dpi=80)
  16. # plt.bar(range(len(_x)),_y,width=0.3,color="orange")
  17. plt.barh(range(len(_x)),_y,height=0.3,color="orange")
  18. plt.yticks(range(len(_x)),_x,fontproperties=my_font)
  19. plt.show()

显示结果:

6.索引和复合索引:

6.有全球排名靠前的10000本书的数据,统计不同年份的数量,不同年份书的平均评分情况:

  1. #encoding=utf-8
  2. from matplotlib import pyplot as plt
  3. import numpy as np
  4. import pandas as pd
  5. file_path = "./youtube_video_data/books.csv"
  6. df = pd.read_csv(file_path)
  7. # print(df.head(2))
  8. # print(df.info())
  9. # data1 = df[pd.notnull(df["original_publication_year"])]
  10. # grouped = data1.groupby(by="original_publication_year").count().title
  11. # print(grouped)
  12. #不同年份书的平均评分情况
  13. #取出original_publication_year列中nan行
  14. data1 = df[pd.notnull(df["original_publication_year"])]
  15. grouped = data1["average_rating"].groupby(by=data1["original_publication_year"]).mean()
  16. #print(grouped)
  17. _x = grouped.index
  18. _y = grouped.values
  19. #画图
  20. plt.figure(figsize=(20,8),dpi=80)
  21. plt.plot(range(len(_x)),_y)
  22. plt.xticks(range(len(_x))[::10],_x[::10].astype(int),rotation=90)
  23. #plt.xticks(list(range(len(_x)))[::100],_x[::100],rotation=90)
  24. plt.show()
显示结果:

 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/Gausst松鼠会/article/detail/244052
推荐阅读
相关标签
  

闽ICP备14008679号