pandas库
pandas是一个操作数据的一个库
以泰塔尼克号船员为例(数据集可以上github上下载)
- 读取数据集
titanic_surv=pd.read_csv("titanic_train.csv")
- 显示数据集的指定一列
age=titanic_surv["Age"]#显示年龄这一列
- 判断数据是否为null,null值为True,否则为False
age_is_null=pd.isnull(age)
age_is_null
显示出结果:
0 False
1 False
2 False
3 False
4 False
...
886 False
887 False
888 True
889 False
890 False
Name: Age, Length: 891, dtype: bool
显示age值为null数据,age[age_is_null]中age_is_null的参数默认为true
age_null_true=age[age_is_null]
age_null_true
len()函数是统计数据的函数
查找age不为null,参数要写age_is_null=False
titanic_surv["Age"][age_is_null==False]
- 求平均数的几种方法,可以对比参照学习
age_mean=sum(age_null_false)/len(age_null_false)
age_mean
------------------------
titanic_surv["Age"][age_is_null==False].mean()
--------------------------
passenger_surv=titanic_surv.pivot_table(index="Pclass",values="Survived",aggfunc=np.mean)
passenger_surv
-------------------------------
passenger_age=titanic_surv.pivot_table(index="Pclass",values="Age")
passenger_age
#这个也是求平均数,与上一种比较aggfunc参数默认