Pandas数据操作学习笔记

1、df.country.unique()
表示dataframe中的country列中没有重复的字段,也就是共有哪些国家

2、 df.country.value_counts()
统计country列中不同国家出现的次数

3、表示dataframe中description列中每个字段是否含有tropical,注意map用法

tropical_wine = df.description.map(lambda r: "tropical" in r).value_counts()

4、df.loc[(df.country.notnull()) & (df.variety.notnull())]
选择df中country和variety列不是NaN的数据

5、df = df.dropna(subset=['country','variety'])
删掉country和variety列中NaN数据,注意要赋值,因为操作不会直接在原有的df上删减

6、下面这个两个效果一样,注意agg的用法

pd.concat([df.groupby('variety').price.min().rename('min'),df.groupby('variety').price.max().rename('max')],axis=1)

df.groupby('variety').price.agg([min, max])

7、 df.groupby('price').points.max().sort_index()
表示把price分组,然后列出每组的points的最大值,最后price从小到大排列

8、df.rename_axis("wines", axis="rows")
更改index名字为‘wines’

9、df.rename(columns={'region_1':'region','region_2':'locale'})
更改列名,region_1改为region

10、df.assign(n=0)
增加列名为n的一列,值全是0

11、df.reset_index()
重置index后,原来的index成为列,新的index为0.1.2.3…..

12、df.col.value_counts()
col列中的值出现频率从多到少一次排列

这些method经常连用,有些method的组合能产生意想不到的效果
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值