python的pandas处理数据第一次


一、这是kaggle上面的泰坦尼克号题,看帖子上有数据探索过程,照着做了一下,感觉跟R差不多,应该是我还没有深入学吧。

二、matplotlib的作图子包pyplot先学一下,plt.figure()是定义一个图像,再用figure.add_subplot()方法增加子图,设置图的排列顺序。

import pandas as pd
import matplotlib.pyplot as plt

dt=pd.read_csv('C:\\Users\\Administrator\\Desktop\\tatannic\\train.csv')
#第二个图,年纪和人数的关系
age=dt.Age
mean=age.mean()
age=age.fillna(mean)
fig=plt.figure()
ax=fig.add_subplot(1,2,1)
ax.hist(age,bins=10)
plt.xlabel('Age')
plt.ylabel('Count of people')


#第二个图,船票和人数的关系
fare=dt.Fare
ax=fig.add_subplot(1,2,2)
ax.hist(fare,bins=10)
plt.xlabel('fare')
plt.show()


#画船票和人数的箱子图
fig2=plt.figure()
ax=fig2.add_subplot(1,1,1)
ax.boxplot(fare)
plt.xlabel('fare')

plt.ylabel('Count of people')


三、数据的groupby、

一直按照结合sql语句、R语言,python学习数据处理,基本上思想都是处理一个表。

pandas中的groupby是一个方法,参数是某一列。


>dt.groupby(['Pclass','Survived']).Pclass.count()
Out[52]: 
Pclass  Survived
1       0            80
        1           136
2       0            97
        1            87
3       0           372
        1           119
Name: Pclass, dtype: int64

会按照Pclass和Survived聚合,而且会分别做统计。


统计每个Pclass每一类字段对应的Survived属性的总量。

>dt.groupby('Pclass').Survived.count()
Out[66]: 
Pclass
1    216
2    184
3    491


统计每个Pclass每一类字段对应的Survived属性为1的总量

>dt.groupby(['Pclass']).Survived.sum()
Out[71]: 
Pclass
1    136
2     87
3    119
Name: Survived, dtype: int64















评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

A叶子叶

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值