pandas
pandas之字符串离散化的案例
对于这一组电影数据,如果我们想rating,runtime的分布情况,应该如何呈现数据?
对于这一组电影数据,如果我们希望统计电影分类(genre)的情况,应该如何处理数据?
思路:重新构造一个全为0的数组,列名为分类,如果某一条数据中分类出现过,就让0变为1
"""
对于这一组电影数据,如果我们希望统计电影分类的情况
,应该如何处理数据?
思考:重新构造一个全为0的数组,列名为分类,如果某
数据中分类出现过,就让0变为1
"""
import pandas as pd
from matplotlib import pyplot as plt
import numpy as np
file_path = "IMDB-Movie-Data.csv"
df = pd.read_csv(file_path)
# 统计分类的列表
temp_list = df["Genre"].str.split(",").tolist()
genre_list = list(set([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)
# 给每个电影出现分类的位置赋值:
for i in range(df.shape[0]):
# 举例 zeros_df.loc[0,["sci-fi","Mucical","xxx"]]
zeros_df.loc[i,temp_list[i]] = 1
# print(zeros_df)
# 统计每个分类的电影的数量和
genre_count = zeros_df.sum(axis=0)
genre_count = genre_count.sort_values()
# 画图
_x = genre_count.index
_y = genre_count.values
plt.figure(figsize=(16,8),dpi=80)
plt.bar(range(len(_x)),_y)
plt.xticks(range(len(_x)),_x)
plt.show()
pandas之数据合并join
pandas之数据聚合
在pandas中类似的分组的操作我们有很简单的方式来完成
df.groupby(by=“columns_name”)
那么问题来了,调用groupby方法之后返回的是什么内容?
import pandas as pd
import numpy as np
file_path = "starbucks_store_worldwide.csv"
df = pd.read_csv(file_path)
# print(df.head())
# print(df.info())
grouped = df.groupby(by="Country")
country_count = grouped["Brand"].count()
print(country_count["US"])
print(country_count["CN"])
# count =0
# for i,j in grouped:
# while count<5:
# print(i)
# print("-"*80)
# print(j)
# print("*"*50)
# count +=1
# 统计中国每个省份的店铺数量
# china_data = df[df["Country"]=="CN"]
# grouped = china_data.groupby(by="State/Province")["Brand"].count()
# print(grouped)
# 统计国家和省份
# grouped = df["Brand"].groupby(by=[df["Country"],df["State/Province"]]).count()
# print(grouped)
# 数据按照多个条件进行分组
grouped = df["Brand"].groupby(by=[df["Country"],df["State/Province"]]).count()
print(grouped)
print(type(grouped))
print("-"*100)
print(grouped.index)
# 数据按照多个条件进行分组,返回DataFrame
# grouped1 = df[["Brand"]].groupby(by=[df["Country"],df["State/Province"]]).count()
# grouped2 = df.groupby(by=[df["Country"],df["State/Province"]]).count()[["Brand"]]
# grouped3 = df.groupby(by=[df["Country"],df["State/Province"]])[["Brand"]].count()
# print(grouped1,type(grouped1))
# print(grouped2,type(grouped2))
# print(grouped3,type(grouped3))
pandas之索引和复合索引
使用matplotlib呈现出店铺总数排名前10的国家
import pandas as pd
from matplotlib import pyplot as plt
file_path = "starbucks_store_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=(16,8),dpi=80)
plt.bar(range(len(_x)),_y)
plt.xticks(range(len(_x)),_x)
plt.show()
使用matplotlib呈现出每个中国每个城市的店铺数量
import pandas as pd
import matplotlib
from matplotlib import pyplot as plt, font_manager
my_font = font_manager.FontProperties(fname="C:/Windows/Fonts/simfang.ttf",size=8)
file_path = "starbucks_store_worldwide.csv"
df = pd.read_csv(file_path)
df = df[df["Country"]=="CN"]
# 使用matplotlib呈现出店铺总数排名前十的城市
data1 = df.groupby(by="City").count()["Brand"].sort_values(ascending=False)[:25]
_x = data1.index
_y = data1.values
plt.figure(figsize=(16,8),dpi=80)
plt.bar(range(len(_x)),_y)
plt.xticks(range(len(_x)),_x,fontproperties=my_font)
plt.show()