垂直合并熊猫数据框,而不会使用熊猫丢失列名

时间:2020-10-15 04:51:31

标签: python pandas dataframe


is there any way to merge 3 different dataframes vertically with different columns and index like shown in image using pandas?

df1是第一个数据帧,具有第1,2,8,9列 df2是第二个数据帧,具有第3,4列 df3是第三个数据帧,具有第5、6、7列

1 个答案:

答案 0 :(得分:1)

尝试:

len()

有关更多详细信息,您可以查看Merge, join, concatenate and compare in pandas

示例:

// horizontally
pandas.concat([df1, df2, df3], axis=1)

// vertically
pandas.concat([df1, df2, df3])

输出:

import pandas as pd

df1 = pd.DataFrame({'C1': ['1', '2', '3'], 'C2': ['a', 'b', 'c'], 'C8': ['t', 'u', 'v'], 'C3': ['w', 'x', 'y']})

print (df1)    

df2 = pd.DataFrame({'C3': ['4', '5', '6', '12'], 'C4': ['d', 'e', 'f', 's']})

print (df2)

df3 = pd.DataFrame({'C5': ['7', '8', '9', '10', '11'], 'C6': ['i', 'j', 'k', 'l', 'm'], 'C7': ['n', 'o', 'p', 'q', 'r']})

print (df3)

// horizontally
print (pd.concat([df1, df2, df3], axis=1))

// vertically
print (pd.concat([df1, df2, df3]))