发布于 

pandas基础操作笔记

记录下常用的pandas的操作代码。

Jupyter Notebook环境配置:https://www.zhihu.com/question/40012144/answer/363009024

不推荐使用jupyter-themes,页面越改越丑,我只想改个里面的字体。

1
2
3
4
# python的pandas模块获取日期间隔
import pandas as pd
date_list = [d.strftime("%Y%m%d") for d in pd.date_range('2020-05-25', end='2020-06-07', freq="d")]
date_list
1
2
3
4
5
6
7
# pandas计算唯一值个数
dferr.groupby(by=['ds']).userid.nunique()
dferr.pivot_table(index=['ds'],columns=['user_level'],values=['userid'],aggfunc=lambda x:len(x.unique()),margins=True)

# pandas按照百分比展示
df2.div(df2.iloc[-1,-1],axis=0).applymap(lambda x:format(x,'.1%'))
df3.div(df3.iloc[:,-1],axis=0).applymap(lambda x:format(x,'.1%'))

Jupyter Notebook笔记

最后再来个pandas官方出的速查图:

github地址:Pandas_Cheat_Sheet

pandas官方速查