1.读取csv文件
import numpy as np
import pandas as pd
data=pd.read_csv('C:/Users/john/Desktop/student.csv')
print(data)
2.导出csv文件
import numpy as np
import pandas as pd
data=pd.read_csv('C:/Users/john/Desktop/student.csv')
data.to_csv('C:/Users/john/Desktop/my_student.csv')
1.纵向合并,重新给定行索引
import numpy as np
import pandas as pd
df1=pd.DataFrame(np.ones((3,4))*0,columns=['a','b','c','d'])
df2=pd.DataFrame(np.ones((3,4))*1,columns=['a','b','c','d'])
df3=pd.DataFrame(np.ones((3,4))*2,columns=['a','b','c','d'])
#0表示纵向 1表示横向 ignore_index表示行的索引重新排序
res=pd.concat([df1,df2,df3],axis=0,ignore_index=True)
print(res)
2.john------[‘inner’,‘outer’]
import numpy as np
import pandas as pd
df1=pd.DataFrame(np.ones((3,4))*0,columns=['a','b','c','d'],index=[1,2,3])
df2=pd.DataFrame(np.ones((3,4))*1,columns=['b','c','d','e'],index=[2,3,4])
#默认john模式是outer 指的是columns
#outer 缺失部分用nan填充
res=pd.concat([df1,df2],join='outer')
#inner 只保留都有的部分
#res=pd.concat([df1,df2],join='inner')
print(res)
3.append
import numpy as np
import pandas as pd
df1=pd.DataFrame(np.ones((3,4))*0,columns=['a','b','c','d'],index=[1,2,3])
df2=pd.DataFrame(np.ones((3,4))*1,columns=['b','c','d','e'],index=[2,3,4])
res=df1.append([df2,df3],ignore_index=True)
print(res)
1.参数on(依据)
import numpy as np
import pandas as pd
left=pd.DataFrame({'key':['K0','K1','K2','K3'], 'A':['A0','A1','A2','A3'], 'B':['B0','B1','B2','B3']})
right=pd.DataFrame({'key':['K0','K1','K2','K3'], 'C':['C0','C1','C2','C3'], 'D':['D0','D1','D2','D3']})
#依据key合并
#how={'inner','outer','left','right'}
#indicator=True False 是否提示合并依赖项
res=pd.merge(left,right,on='key')
print(res)
2.xxx_index=True 考虑行标号合并(默认是列)
import numpy as np
import pandas as pd
left=pd.DataFrame({'A':['A0','A1','A2'], 'B':['B0','B1','B2']},index=['K0','K1','K2'])
right=pd.DataFrame({ 'C':['C0','C1','C2'], 'D':['D0','D1','D2']},index=['K1','K2','K3'])
res=pd.merge(left,right,left_index=True,right_index=True,how='outer')
print(res)
3.suffixes—以列标号合并有所不同
import numpy as np
import pandas as pd
boys=pd.DataFrame({'k':['K0','K1','K2'],'age':[1,2,3]})
girls=pd.DataFrame({'k':['K0','K1','K2'],'age':[4,5,6]})
res=pd.merge(boys,girls,on='k',suffixes=['_boy','_girl'],how='outer')
print(res)
本文参考资料-----【莫烦Python】 系列教程