将新的Pandas DataFrame附加到旧的Pandas DataFrame,而不对列名进行排序

时间:2016-03-21 02:35:31

标签: python pandas

我将新数据框附加到旧数据框中:

import numpy as np
import pandas as pd
from pandas import Series
from pandas import DataFrame

df1 = DataFrame(np.arange(3.).reshape((1, 3)), columns=list('dcb'), index=['Ohio'])
df2 = DataFrame(np.arange(3.).reshape((1, 3)), columns=list('bdc'), index=['Utah'])
print df1
print df2
print pd.concat([df1, df2])

然后我得到了这样的结果:

       d    c    b
Ohio  0.0  1.0  2.0

       b    d    c
Utah  0.0  1.0  2.0

       b    c    d
Ohio  2.0  1.0  0.0
Utah  0.0  2.0  1.0

但是我希望结果中的列不被排序为' bcd'但作为起源' dcb'像:

       d    c    b
Ohio  0.0  1.0  2.0
Utah  1.0  2.0  0.0

2 个答案:

答案 0 :(得分:3)

使用join_axes参数:

pd.concat([df1, df2], join_axes=[df1.columns])

答案 1 :(得分:2)

您可以将原始订单存储在变量中,然后在合并后重新应用:

df1 = DataFrame(np.arange(3.).reshape((1, 3)), columns=list('dcb'), index=['Ohio'])
orig_column_order = df1.columns
df2 = DataFrame(np.arange(3.).reshape((1, 3)), columns=list('bdc'), index=['Utah'])
combined = pd.concat([df1, df2], keys=list('dbc'))
combined = combined[orig_column_order]
print(df1)
print(df2)
print(combined)

给出:

        d    c    b
Ohio  0.0  1.0  2.0
        b    d    c
Utah  0.0  1.0  2.0
          d    c    b
d Ohio  0.0  1.0  2.0
b Utah  1.0  2.0  0.0