import numpy as np
import pandas as pd
dates = np.arange(20170101,20170105)
df1 = pd.DataFrame(np.arange(12).reshape((4,3)),index=dates,
columns=['A','B','C'])
df1
df2 = pd.DataFrame(df1,index=dates,columns=['A','B','C','D','E'])
df2
s1 = pd.Series([3,4,6],index=dates[:3])
s2 = pd.Series([32,5,2],index=dates[1:])
df2['D'] = s1
df2['E'] = s2
df2
#把有空的行去掉
df2.dropna(axis=0,how='any')
#axis=[0,1],0代表行,1代表列
#how可以取两个值,any或all,any代表任意一个或多个0,all代表全部
#把有空的行去掉
df2.dropna(axis=0,how='all')
#axis=[0,1],0代表行,1代表列
#how可以取两个值,any或all,any代表任意一个或多个0,all代表全部为0
#把有空的列去掉
df2.dropna(axis=1,how='any')
#axis=[0,1],0代表行,1代表列
#how可以取两个值,any或all,any代表任意一个或多个0,all代表全部为0
df2.fillna(value=0)#表中所有的空值赋值为0
df2.isnull()#判断是否有空值
np.any(df2.isnull())
#判断是否有空值,有的话返回true,否返回false
np.all(df2.isnull())
#所有为空才返回true
pandas读取及写入文件
file = pd.read_excel('people.xlsx')
file
file = pd.read_csv('people.csv',encoding='gbk')
file
file.iloc[2,0] = '深圳' #将第2列第0行改为深圳
file
file.to_csv('people2.csv')#将文件转为不同的格式
pandas合并
df1 = pd.DataFrame(np.arange(12).reshape((3,4)),
columns=['a','b','c','d'])
df2 = pd.DataFrame(np.arange(12,24).reshape((3,4)),
columns=['a','b','c','d'])
df3 = pd.DataFrame(np.arange(24,36).reshape((3,4)),
columns=['a','b','c','d'])
print(df1)#为什么写print而不是直接写df1,df2,df3?
print(df2)#因为这样写会覆盖掉,只显示df3
print(df3)
df4 = pd.concat([df1,df2,df3],axis=0)#axis=0代表纵向合并,行合并
df4
df4 = pd.concat([df1,df2,df3],axis=0,ignore_index=True)
#axis=0代表纵向合并,行合并
#ignore_index=True表示忽略原来的index属性
df4
df5 = pd.concat([df1,df2,df3],axis=1)
#axis=1代表横向合并,列合并
df5
df1 = pd.DataFrame(np.arange(12).reshape((3,4)),
columns=['a','b','c','f'])
df2 = pd.DataFrame(np.arange(12,24).reshape((3,4)),
columns=['a','b','d','e'])
print(df1)
print(df2)
df6 = pd.concat([df1,df2],join='outer',ignore_index=True)
#合并两个表,缺少的部分填充none
df6
df7 = pd.concat([df1,df2],join='inner',ignore_index=True)
#合并两个表共有的部分
df7
df1 = pd.DataFrame(np.arange(12).reshape((3,4)),
columns=['a','b','c','f'])
df2 = pd.DataFrame(np.arange(12,24).reshape((4,3)),
columns=['a','c','d'])
print(df1)
print(df2)
#df8 = pd.concat([df1,df2],axis=1,join_axes=[df1.index])
#横向合并,index使用df1的index
#新版本pandas已经删除了join_axes
#所以习惯了两个dataframe按照同一列合并的话,可以改用merge
df8 = pd.concat([df1,df2],axis=1)#横向合并,列合并
df8