import pandas as pd
import numpy as np
food_info = pd.read_csv('food_info.csv')
1.处理缺失值(可使用平均数,众数填充)
查看非缺失值的数据:
price_is_null = pd.isnull(food_info["Price"])
price = food_info["Price"][price_is_null==False]
使用 fillna 填充
food_info['Price'].fillna(food_info['Price'].mean(),inplace = True)
2.求平均值
food_info["Price"].mean()
3.查看每一个 index 级,values 的平均值
food_info.pivot(index = "",values = "",aggfunc = np.mean)
4.查看总人数
food_info.pivot(index = "",values = ["",""],aggfunc = np.sum)
5.丢弃缺失值
dropna_columns = food_info.dropna(axis = 1)
将 Price 和 Time 列存在 NaN 的行去掉
new_food_info = food_info.dropna(axis = 0,subset = ["Price","Time"])
6.定位具体值到 83
row_index_83_price = food_info.loc[83,"Price"]
7.进行排序(sort_values 默认升序)
new_food_info.sort_values("Price")
8.将索引值重新排序,使用 reset_index
new_food_info.reset_index(drop = True)
9.使用 apply 函数
new_food_info.apply(函数名)
10.查看缺失值的个数
def not_null_count(column):
column_null = pd.isnull(column)
# column_null 为空的布尔类型
null = column[column_null]
# 将为空值的列表传递给 null
return len(null)
column_null_count = food_info.apply(not_null_count)
11.划分等级:年龄 成绩
def which_class(row):
pclass = row["Pclass"]
if pd.isnull(pclass):
return "未知等级"
elif pclass == 1:
return "第一级"
elif pclass == 2:
return "第二级"
elif pclass == 3:
return "第三级"
new_food_info.apply(which_class,axis = 1)
12.使用 pivot_table 展示透视表
new_food_info.pivot_table(index = " ",values=" ")
2020-04-11