# 数据存储和调用 import numpy as np import pandas as pd # data = pd.read_csv('E:\pythonDemo\Pandas\data\data.csv') # # 读取csv文件 # data.to_pickle('E:\pythonDemo\Pandas\data\data.pickle') # # 储存文件 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']) df4 = pd.DataFrame(np.arange(36,48).reshape((3,4)), index=[2, 3, 4], columns=['b', 'c', 'd', 'e']) res = pd.concat([df1, df2, df3],axis=0,ignore_index=True) print(res) # 合并,通过axis控制合并类型,默认为纵向合并,ignore_index参数为True是重新建立索引防止索引重复产生问题 res = pd.concat([df1, df4], axis=0, ignore_index=True, join='inner') # 在两个矩阵中存在部分不同的索引时候,使用join参数进行合并,有inner和outer方式 默认为outer,外连接,inner,内连接 print(res) ''' outer a b c d e 0 0.0 1 2 3 NaN 1 4.0 5 6 7 NaN 2 8.0 9 10 11 NaN 3 NaN 36 37 38 39.0 4 NaN 40 41 42 43.0 5 NaN 44 45 46 47.0 ------------------------ inner b c d 0 1 2 3 1 5 6 7 2 9 10 11 3 36 37 38 4 40 41 42 5 44 45 46 ''' # res = pd.concat([df1, df4], axis=1, join_axes = [df1.index]) #现版本pandas已弃用join_axes参数 res = df1.append([df2, df3],ignore_index=True) print(res) # append 增加 将df1与df2纵向连接 s1 = pd.Series([1, 2, 3, 4],index =['a', 'b', 'c', 'd']) res = df1.append(s1,ignore_index=True) print(res) # 相当于在df1下面添加一行列表
关于Pandas的基本用法_05
最新推荐文章于 2024-04-26 19:13:06 发布