本文摘要:

使用concat拼接数据
df1为  df2为 
- 默认concat,参数axis=0, join=‘outer’, ignore_index=False,即按行拼接,拼接方式保留所有列,不忽略原数据索引。
import pandas as pd
pd.concat([df1, df2])
返回结果 
- 设置ignore_index=True,忽略原数据索引
import pandas as pd
pd.concat([df1, df2], ignore_index=True)
返回结果,可以看到行索引的区别。  3. 设置join=‘inner’,只保留共有列(因为默认axis=0是按行拼接,所以保留是考虑列)
import pandas as pd
pd.concat([df1, df2], ignore_index=True, join='inner')
返回结果 
- 设置axis=1,也就是按列拼接,即添加一列或多列属性。
s1 = pd.Series(list(range(4)), name='F')
pd.concat([df1, s1], axis=1)
返回结果,因为axis=1,join默认’outer’,ignore_index默认False,即按列拼接,保留所有行,保留原数据索引 
s2 = df1.apply(lambda x:x['A']+'_GG', axis=1)
s2.name='G'
pd.concat([df1, s1, s2], axis=1)
s2如下  返回结果  concat的列表的参数,可以DataFrame 和 Series混合放,返回的结果顺序与列表的一致。
pd.concat([s1, s2], axis=1)
返回结果 
pd.concat([s1, df1, s2], axis=1)
返回结果 
使用append按行拼接
df1为  df2为 
- DataFrame按行拼接DataFrame
df1.append(df2)

- 忽略原数据索引
df1.append(df2, ignore_index=True)
 *此文仅为个人笔记
|