pandas Concat正在删除原始数据

mzsu5hc0  于 2023-01-07  发布在  其他
关注(0)|答案(1)|浏览(148)

两个 Dataframe 具有相同数量的列和相同的列名。我正在尝试重新排序列名,以便可以将它们连接起来。请参见下面的示例:
金融服务部

df1 = [a,b,c]
df2 = [a,c'b]

代码:

column_order = [a,b,c]
df1= df1[column_order]
df2 = df2[column_order]
df = pd.concat([df1, df2], axis=0, ignore_index=True)

在本例中,当df1和df2连接在一起时,df2中的"c"列将只显示零。

brvekthn

brvekthn1#

对我来说效果很好:

import pandas as pd
import numpy as np

data1 = np.random.randint(low=0, high=19, size=(2, 3))
data2 = np.random.randint(low=0, high=19, size=(2, 3))

df1 = pd.DataFrame(columns=['a','b','c'], data=data1)
df2 = pd.DataFrame(columns=['a','c','b'], data=data2)

column_order = ['a','b','c']
df1= df1[column_order]
df2 = df2[column_order]
df = pd.concat([df1, df2], axis=0, ignore_index=True)
df

    a   b   c
0   9   4   15
1   0   17  16
0   17  9   8
1   0   8   10

但是,如果列名不匹配,则会得到NaN值:

import pandas as pd
import numpy as np

np.random.seed(10)
data1 = np.random.randint(low=0, high=19, size=(2, 3))
data2 = np.random.randint(low=0, high=19, size=(2, 3))

df1 = pd.DataFrame(columns=['a','b','c2'], data=data1)
df2 = pd.DataFrame(columns=['a','c','b'], data=data2)

column_order = ['a','b','c']
# df1= df1[column_order]
# df2 = df2[column_order]
df = pd.concat([df1, df2], axis=0)
df

    a   b   c2       c
0   9   4   15.0    NaN
1   0   17  16.0    NaN
0   17  9   NaN 8.0
1   0   8   NaN 10.0

相关问题