目录
pandas常用统计方法
import pandas as pd
file_path = 'Data.csv'
df = pd.read_csv(file_path)
# 获取平均分
df["Rating"].mean()
# 导演的人数
# print(len(set(df["Director"].tolist())))
#unique() 唯一的意思,并且可以将数据变为列表
print(len(df["Director"].unique()))
# 获取演员的人数
temp_actors_list = df['Actor'].str.split(',').tolist()
actors_list = [i for j in temp_actors_list for i in j]
actors_num = len(set(actors_list)
print(actors_num)
字符串离散化
import pandas as pd
import numpy as np
file_path = 'Data.csv'
df = pd.read_csv(file_path)
temp_list = df['Genre'].str.split(',').tolist() # [[],[],[]]
genre_list = [i for j in temp_list for i in j]
# 构造全为0的数组
zeros_df = pd.DataFrame(np.zeros((df.shape[0],len(genre_list))),columns=genre_list)
# 给每个电影出现分类的位置赋值1
for i in range(df.shape[0]):
#zero_df.loc[0,['Sci-fi','Mucical']] = 1
zeros_df.loc[i, temp_list[i]] = 1
# 统计每个分类的电影的数量和
genre_count = zeros_df.sum(axis=0)
print(genere_count)
# 排序
genre_count = genre_count.sort_values()
_x = genre_count.index
_y = genre_count.values
#画图
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(len(_x)),y)
plt.xticks(range(len(_x)),_x)
plt.show()
数据合并
数据合并之join
数据合并之merge
数据分组聚合
import pandas as pd
import numpy as np
file_path = 'worldwide.csv'
df = pd.read_csv(file_path)
grouped = df.groupby(by='Country')
# 可以进行遍历
# for i in grouped:
# print(i)
# 调用聚合方法
country_count = grouped['Brand'].count()
print(country_count['US'])
print(country_count['CN '])
# 统计中国每个省店铺的数量
china_data = df[df["Country" == "CN"]
grouped = china_data.groupby(by="State/Province").count()["Brand"]
# 数据按照多个条件进行分组,返回Series
grouped = df["Brand"].groupby(by=[df["Country"],df["State/Province"]]).count()
# 数据按照多个条件进行分组,返回Dataframe
grouped1 = df[["Brand"]].groupby(by=[df["Country"],df["State/Province"]]).count()
grouped2 = df.groupby(by=[df["Country"],df["State/Province"]])[["Brand"]].count()
grouped3 = df.groupby(by=[df["Country"],df["State/Province"]]).count()[["Brand"]]
索引和复合索引
import pandas as pd
from matplotlib import pyplot as plt
file_path = 'worldwide.csv'
df = pd.read_csv(file_path)
#使用matplotlib呈现出店铺总数排名前10名的国家
#准备数据
data1 = df.groupby(by="Country").count()["Brand"].sort_values(ascending=False)[:10]
_x = data1.index
_y = data1.values
#画图
plt.figure(figsize=(20,8),dpi=80)
plt.bar(range(len(_x)),_y)
plt.xticks(range(len(_x)),_x)
plt.show()
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(frame="Songti.ttc")
file_path = 'worldwide.csv'
df = pd.read_csv(file_path)
df = df[df["Country"]=="CN"]
#使用matplotlib呈现出店铺总数排名前10名的国家
#准备数据
data1 = df.groupby(by="City").count()["Brand"].sort_values(ascending=False)[:25]
_x = data1.index
_y = data1.values
#画图
plt.figure(figsize=(20,12),dpi=80)
#plt.bar(range(len(_x)),_y,width=0.3,color="orange")
plt.barh(range(len(_x)),_y,height=0.3,color="orange")
plt.yticks(range(len(_x)),_x,fontproperties=my_font)
plt.show()
import pandas as pd
from matplotlib import pyplot as plt
from matplotlib import font_manager
my_font = font_manager.FontProperties(frame="Songti.ttc")
file_path = './books.csv'
df = pd.read_csv(file_path)
# data1 = df[pd.notnull(df["original_publication_year"])]
# grouped = data1.groupby(by="original_publication_year").count()["title"]
#不同年份书的平均评分情况
#去除original_publication_year列中nan的行
data1 = df[pd.notnull(df["original_publication_year"])]
grouped = data1["average_rating"].groupby(by="original_publication_year").mean()
_x = grouped.index
_y = grouped.values
#画图
plt.figure(figsize=(20,8),dpi=80)
plt.plot(range(len(_x)),_y)
plt.xticks(list(range(len(_x)))[:10],_x[::10].astype(int),rotation=45)
plt.show()