In [11]: df2 Out[11]: A B C D E F 0 1 2013-01-02 1 3 test foo 1 1 2013-01-02 1 3 train foo 2 1 2013-01-02 1 3 test foo 3 1 2013-01-02 1 3 train foo
每列的格式用 dtypes 查看
1 2 3 4 5 6 7 8 9
In [12]: df2.dtypes Out[12]: A float64 B datetime64[ns] C float32 D int32 E category F object dtype: object
你可以认为,DataFrame 是由 Series 组成的
1 2 3 4 5 6 7
In [13]: df2.A Out[13]: 0 1 1 1 2 1 3 1 Name: A, dtype: float64
查看数据
用 head 和 tail 查看顶端和底端的几列
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
In [14]: df.head() Out[14]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401
In [15]: df.tail(3) Out[15]: A B C D 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 2013-01-06 -0.673690 0.113648 -1.478427 0.524988
实际上,DataFrame 内部用 numpy 格式存储数据。你也可以单独查看 index
和 columns
In [24]: df[0:3] Out[24]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
In [25]: df['20130102':'20130104'] Out[25]: A B C D 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 2013-01-04 0.721555 -0.706771 -1.039575 0.271860
In [26]: df.loc[dates[0]] Out[26]: A 0.469112 B -0.282863 C -1.509059 D -1.135632 Name: 2013-01-01 00:00:00, dtype: float64
还可以多选
1 2 3 4 5 6 7 8 9
In [27]: df.loc[:,['A','B']] Out[27]: A B 2013-01-01 0.469112 -0.282863 2013-01-02 1.212112 -0.173215 2013-01-03 -0.861849 -2.104569 2013-01-04 0.721555 -0.706771 2013-01-05 -0.424972 0.567020 2013-01-06 -0.673690 0.113648
注意那个冒号,用法和 MATLAB 或 NumPy 是一样的!所以也可以这样
1 2 3 4 5 6
In [28]: df.loc['20130102':'20130104',['A','B']] Out[28]: A B 2013-01-02 1.212112 -0.173215 2013-01-03 -0.861849 -2.104569 2013-01-04 0.721555 -0.706771
依旧和 MATLAB
一样,当有一个维度是标量(而不是范围或序列)的时候,选择出的矩阵维度会减少
1 2 3 4 5
In [29]: df.loc['20130102',['A','B']] Out[29]: A 1.212112 B -0.173215 Name: 2013-01-02 00:00:00, dtype: float64
如果对所有的维度都写了标量,不就是选出一个元素吗?
1 2
In [30]: df.loc[dates[0],'A'] Out[30]: 0.46911229990718628
这种情况通常用 at ,速度更快
1 2
In [31]: df.at[dates[0],'A'] Out[31]: 0.46911229990718628
通过整数下标选择
和 MATLAB 完全一样
这个就和数组类似啦,直接看例子。选出第3行:
1 2 3 4 5 6 7
In [32]: df.iloc[3] Out[32]: A 0.721555 B -0.706771 C -1.039575 D 0.271860 Name: 2013-01-04 00:00:00, dtype: float64
选出34行,01列:
1 2 3 4 5
In [33]: df.iloc[3:5,0:2] Out[33]: A B 2013-01-04 0.721555 -0.706771 2013-01-05 -0.424972 0.567020
也能用 list 选择
1 2 3 4 5 6
In [34]: df.iloc[[1,2,4],[0,2]] Out[34]: A C 2013-01-02 1.212112 0.119209 2013-01-03 -0.861849 -0.494929 2013-01-05 -0.424972 0.276232
也能用 slice
1 2 3 4 5
In [35]: df.iloc[1:3,:] Out[35]: A B C D 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804
1 2 3 4 5 6 7 8 9
In [36]: df.iloc[:,1:3] Out[36]: B C 2013-01-01 -0.282863 -1.509059 2013-01-02 -0.173215 0.119209 2013-01-03 -2.104569 -0.494929 2013-01-04 -0.706771 -1.039575 2013-01-05 0.567020 0.276232 2013-01-06 0.113648 -1.478427
对应单个元素
1 2
In [37]: df.iloc[1,1] Out[37]: -0.17321464905330858
1 2
In [38]: df.iat[1,1] Out[38]: -0.17321464905330858
布尔值下标
和 MATLAB 类似
基本用法
1 2 3 4 5 6
In [39]: df[df.A > 0] Out[39]: A B C D 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 2013-01-04 0.721555 -0.706771 -1.039575 0.271860
没有填充的值等于 NaN
1 2 3 4 5 6 7 8 9
In [40]: df[df > 0] Out[40]: A B C D 2013-01-01 0.469112 NaN NaN NaN 2013-01-02 1.212112 NaN 0.119209 NaN 2013-01-03 NaN NaN NaN 1.071804 2013-01-04 0.721555 NaN NaN 0.271860 2013-01-05 NaN 0.567020 0.276232 NaN 2013-01-06 NaN 0.113648 NaN 0.524988
isin() 函数:是否在集合中
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
In [41]: df2 = df.copy()
In [42]: df2['E'] = ['one', 'one','two','three','four','three']
In [43]: df2 Out[43]: A B C D E 2013-01-01 0.469112 -0.282863 -1.509059 -1.135632 one 2013-01-02 1.212112 -0.173215 0.119209 -1.044236 one 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 two 2013-01-04 0.721555 -0.706771 -1.039575 0.271860 three 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 four 2013-01-06 -0.673690 0.113648 -1.478427 0.524988 three
In [44]: df2[df2['E'].isin(['two','four'])] Out[44]: A B C D E 2013-01-03 -0.861849 -2.104569 -0.494929 1.071804 two 2013-01-05 -0.424972 0.567020 0.276232 -1.087401 four
Setting
为 DataFrame 增加新的列,按 index 对应
1 2 3 4 5 6 7 8 9 10 11 12 13
In [45]: s1 = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130102', periods=6))
In [55]: df1 = df.reindex(index=dates[0:4], columns=list(df.columns) + ['E'])
In [56]: df1.loc[dates[0]:dates[1],'E'] = 1
In [57]: df1 Out[57]: A B C D F E 2013-01-01 0.000000 0.000000 -1.509059 5 NaN 1 2013-01-02 1.212112 -0.173215 0.119209 5 1 1 2013-01-03 -0.861849 -2.104569 -0.494929 5 2 NaN 2013-01-04 0.721555 -0.706771 -1.039575 5 3 NaN
丢弃有 NaN 的行
1 2 3 4
In [58]: df1.dropna() Out[58]: A B C D F E 2013-01-02 1.212112 -0.173215 0.119209 5 1 1
填充缺失值
1 2 3 4 5 6 7
In [59]: df1.fillna(value=5) Out[59]: A B C D F E 2013-01-01 0.000000 0.000000 -1.509059 5 5 1 2013-01-02 1.212112 -0.173215 0.119209 5 1 1 2013-01-03 -0.861849 -2.104569 -0.494929 5 2 5 2013-01-04 0.721555 -0.706771 -1.039575 5 3 5
获取布尔值的 mask:哪些值是 NaN
1 2 3 4 5 6 7
In [60]: pd.isnull(df1) Out[60]: A B C D F E 2013-01-01 False False False False True False 2013-01-02 False False False False False False 2013-01-03 False False False False False True 2013-01-04 False False False False False True
操作
统计
通常,操作都会把 NaN 排除在外
平均值
1 2 3 4 5 6 7 8
In [61]: df.mean() Out[61]: A -0.004474 B -0.383981 C -0.687758 D 5.000000 F 3.000000 dtype: float64
In [87]: df Out[87]: A B C D 0 foo one -1.202872 -0.055224 1 bar one -1.814470 2.395985 2 foo two 1.018601 1.552825 3 bar three -0.595447 0.166599 4 foo two 1.395433 0.047609 5 bar two -0.392670 -0.136473 6 foo one 0.007207 -0.561757 7 foo three 1.928123 -1.623033
做 Group 操作并对每组求和
1 2 3 4 5 6
In [88]: df.groupby('A').sum() Out[88]: C D A bar -2.802588 2.42611 foo 3.146492 -0.63958
可以对两列进行 Group by 并求和
1 2 3 4 5 6 7 8 9 10
In [89]: df.groupby(['A','B']).sum() Out[89]: C D A B bar one -1.814470 2.395985 three -0.595447 0.166599 two -0.392670 -0.136473 foo one -1.195665 -0.616981 three 1.928123 -1.623033 two 2.414034 1.600434
In [91]: index = pd.MultiIndex.from_tuples(tuples, names=['first', 'second'])
In [92]: df = pd.DataFrame(np.random.randn(8, 2), index=index, columns=['A', 'B'])
In [93]: df2 = df[:4]
In [94]: df2 Out[94]: A B first second bar one 0.029399 -0.542108 two 0.282696 -0.087302 baz one -1.575170 1.771208 two 0.816482 1.100230
stack() 把 DataFrame 的列“压缩”到 index 里去
1 2 3 4 5 6 7 8 9 10 11 12 13 14
In [95]: stacked = df2.stack()
In [96]: stacked Out[96]: first second bar one A 0.029399 B -0.542108 two A 0.282696 B -0.087302 baz one A -1.575170 B 1.771208 two A 0.816482 B 1.100230 dtype: float64
反之,只要是 MultiIndex 都可以用 unstack()
恢复出列,默认把最后一个 index 解开
In [97]: stacked.unstack() Out[97]: A B first second bar one 0.029399 -0.542108 two 0.282696 -0.087302 baz one -1.575170 1.771208 two 0.816482 1.100230
In [98]: stacked.unstack(1) Out[98]: second one two first bar A 0.029399 0.282696 B -0.542108 -0.087302 baz A -1.575170 0.816482 B 1.771208 1.100230
In [99]: stacked.unstack(0) Out[99]: first bar baz second one A 0.029399 -1.575170 B -0.542108 1.771208 two A 0.282696 0.816482 B -0.087302 1.100230
In [101]: df Out[101]: A B C D E 0 one A foo 1.418757 -0.179666 1 one B foo -1.879024 1.291836 2 two C foo 0.536826 -0.009614 3 three A bar 1.006160 0.392149 4 one B bar -0.029716 0.264599 5 one C bar -1.146178 -0.057409 6 two A foo 0.100900 -1.425638 7 three B foo -1.035018 1.024098 8 one C foo 0.314665 -0.106062 9 one A bar -0.773723 1.824375 10 two B bar -1.170653 0.595974 11 three C bar 0.648740 1.167115
pivot 是把原来的数据(values)作为新表的行(index)、列(columns)
1 2 3 4 5 6 7 8 9 10 11 12 13
In [102]: pd.pivot_table(df, values='D', index=['A', 'B'], columns=['C']) Out[102]: C bar foo A B one A -0.773723 1.418757 B -0.029716 -1.879024 C -1.146178 0.314665 three A 1.006160 NaN B NaN -1.035018 C 0.648740 NaN two A NaN 0.100900 B -1.170653 NaN C NaN 0.536826
时间序列
pandas 的时间序列功能在金融应用中很有用。
resample 功能:
1 2 3 4 5 6 7 8 9
In [103]: rng = pd.date_range('1/1/2012', periods=100, freq='S')
In [104]: ts = pd.Series(np.random.randint(0, 500, len(rng)), index=rng)
In [127]: df["grade"] Out[127]: 0 very good 1 good 2 good 3 very good 4 very good 5 very bad Name: grade, dtype: category Categories (5, object): [very bad, bad, medium, good, very good]
做 group by 的时候,空的类别也会被呈现出来
1 2 3 4 5 6 7 8 9
In [129]: df.groupby("grade").size() Out[129]: grade very bad 1 bad 0 medium 0 good 2 very good 3 dtype: int64
In [140]: df.to_excel('foo.xlsx', sheet_name='Sheet1')
In [141]: pd.read_excel('foo.xlsx', 'Sheet1', index_col=None, na_values=['NA'])
坑
1 2 3 4 5
>>> if pd.Series([False, True, False]): print("I was true") Traceback ... ValueError: The truth value of an array is ambiguous. Use a.empty, a.any() or a.all().