最近需要对Excel进行一些操作,Pandas无疑是一个很强大的一个工具,搞起来!!
pandas是一个构建在Python编程语言之上快速、强大、灵活和易于使用的开源数据分析和操作工具。
pandas官网
pandas文档
pip install pandas
(生成的数据data类似于excle 有行标签 index 有列标签 columns )
import pandas as pd
import numpy as np
s = pd.Series([1,3,6,np.nan,4,1]) #生成一维数组
0 1.0
1 3.0
2 6.0
3 NaN
4 4.0
5 1.0
dtype: float64
dates = pd.date_range('20160101', periods=6)#index
df = pd.DataFrame(np.random.randn(6,4), index=dates, columns=['A','B','C','D'])# 生成6*4的数据
print(df)
A B C D
2016-01-01 0.766013 0.709325 -0.177668 1.955910
2016-01-02 -0.458004 -1.522308 0.396516 1.358957
2016-01-03 -1.421787 0.978309 0.677042 1.498006
2016-01-04 0.028752 -1.021694 1.245495 -0.316957
2016-01-05 1.972788 -1.803018 -0.110645 -0.635439
2016-01-06 -0.265201 -0.310657 0.262895 1.532657
print(df['A'])# 与 df.A 效果相同 按列标签输出
2013-01-01 -0.162028
2013-01-02 -0.247155
2013-01-03 -1.544164
2013-01-04 -1.194200
2013-01-05 2.173732
2013-01-06 -0.501831
Freq: D, Name: A, dtype: float64
print(df['20130102':'20130104']) # 根据index范围输出 若print(df['A':'C'])则会报错
A B C D
2013-01-02 -0.247155 -1.664754 2.044796 -1.647486
2013-01-03 -1.544164 -0.278302 -0.910734 -1.471865
2013-01-04 -1.194200 -1.295544 -0.522822 0.416136
# select by label: loc 通过 x y 的label 进行选择输出
print(df.loc['20130102'])
print(df.loc[:,['A','B']])
print(df.loc['20130102', ['A','B']])
A -0.247155
B -1.664754
C 2.044796
D -1.647486
Name: 2013-01-02 00:00:00, dtype: float64
A B
2013-01-01 -0.162028 -1.209992
2013-01-02 -0.247155 -1.664754
2013-01-03 -1.544164 -0.278302
2013-01-04 -1.194200 -1.295544
2013-01-05 2.173732 0.422870
2013-01-06 -0.501831 0.392393
A -0.247155
B -1.664754
Name: 2013-01-02 00:00:00, dtype: float64
# select by position: iloc 通过位置选择 即通过坐标选择(或范围)
print(df.iloc[3])
print(df.iloc[3, 1])
print(df.iloc[3:5,0:2])
print(df.iloc[[1,2,4],[0,2]])
A -1.194200
B -1.295544
C -0.522822
D 0.416136
Name: 2013-01-04 00:00:00, dtype: float64
-1.2955436468567092
A B
2013-01-04 -1.194200 -1.295544
2013-01-05 2.173732 0.422870
A C
2013-01-02 -0.247155 2.044796
2013-01-03 -1.544164 -0.910734
2013-01-05 2.173732 -0.665288
# mixed selection: ix 混合选择 即通过label 和坐标 进行选择
print(df.ix[:3, ['A', 'C']])
# Boolean indexing 逻辑选择 可以加入一些逻辑判断进行选择
print(df[df.A > 0])
A C
2013-01-01 -0.162028 -1.259382
2013-01-02 -0.247155 2.044796
2013-01-03 -1.544164 -0.910734
A B C D
2013-01-05 2.173732 0.42287 -0.665288 -0.585846
#与上述差不多 还是确定要改变的数据位置即可
df.iloc[2,2] = 1111# 通过坐标位置改变
df.loc['2013-01-03', 'D'] = 2222 #通过标签确定位置 改变
df.A[df.A>0] = 0 #通过逻辑判断筛选改变
df['F'] = np.nan #通过标签改变
df['G'] = pd.Series([1,2,3,4,5,6], index=pd.date_range('20130101', periods=6))# 666
print(df)
A B C D F G 2013-01-01 0.000000 -0.132451 0.021797 -1.656566 NaN 1 2013-01-02 -0.182227 -0.656873 -0.633688 1.143701 NaN 2 2013-01-03 0.000000 -0.330042 1111.000000 2222.000000 NaN 3 2013-01-04 -0.664301 0.248035 -1.286524 0.879787 NaN 4 2013-01-05 -0.005037 -0.753467 -1.390868 0.842121 NaN 5 2013-01-06 -0.529367 -1.093037 -0.984761 -1.243129 NaN 6
dates = pd.date_range('20130101', periods=6)
df = pd.DataFrame(np.arange(24).reshape((6,4)), index=dates, columns=['A', 'B', 'C', 'D'])
# 先生成几个nan
df.iloc[0,1] = np.nan
df.iloc[1,2] = np.nan
df.iloc[2,:] = np.nan
print(df)
A B C D 2013-01-01 0.0 NaN 2.0 3.0 2013-01-02 4.0 5.0 NaN 7.0 2013-01-03 NaN NaN NaN NaN 2013-01-04 12.0 13.0 14.0 15.0 2013-01-05 16.0 17.0 18.0 19.0 2013-01-06 20.0 21.0 22.0 23.0
print(df.dropna(axis=0, how='any')) # how={'any', 'all'} 删除任何有nan的行 (axis=1则为列)
A B C D 2013-01-04 12.0 13.0 14.0 15.0 2013-01-05 16.0 17.0 18.0 19.0 2013-01-06 20.0 21.0 22.0 23.0
print(df.dropna(axis=0, how='all')) # how={'any', 'all'} 删除全为nan的行 (axis=1则为列)
A B C D 2013-01-01 0.0 NaN 2.0 3.0 2013-01-02 4.0 5.0 NaN 7.0 2013-01-04 12.0 13.0 14.0 15.0 2013-01-05 16.0 17.0 18.0 19.0 2013-01-06 20.0 21.0 22.0 23.0
print(df.fillna(value=0)) #用其它值 填充 nan
A B C D 2013-01-01 0.0 0.0 2.0 3.0 2013-01-02 4.0 5.0 0.0 7.0 2013-01-03 0.0 0.0 0.0 0.0 2013-01-04 12.0 13.0 14.0 15.0 2013-01-05 16.0 17.0 18.0 19.0 2013-01-06 20.0 21.0 22.0 23.0
print(pd.isnull(df)) #判断是不是non
A B C D 2013-01-01 False True False False 2013-01-02 False False True False 2013-01-03 True True True True 2013-01-04 False False False False 2013-01-05 False False False False 2013-01-06 False False False False
小心得:数据 的index可以不写 默认为0 1 2 3…,但是列columns一定要写(写成字典的形式)
参考:https://www.bilibili.com/video/BV1Ex411L7oT