熊猫合并:在特定位置插入列

时间:2019-12-12 23:44:35

标签: python python-3.x pandas dataframe

我有2个数据框:

df_a ["user", "name", "zip", "city"]
df_b ["user", "gender", "country"]

我正在user列上加入这两个数据框-

final_df = pd.merge(df_a, df_b, on='user', how='left')
# column order --> ["user", "name", "zip", "city", "gender", "country"]

但是,我希望这些列按以下顺序排列-

["user", "name", "gender", "country", "zip", "city"]

除了通常的重新排序

my_ordering = ["user", "name", "gender", "country", "zip", "city"]
final_df = final_df[my_ordering]

..什么是获得我想要的顺序的最佳方法(考虑到一个数据帧中可能有1000个这样的列)?

3 个答案:

答案 0 :(得分:0)

您可以这样做:

column_list = list(final_df.columns)

#Now rearrange the list the way you want the columns to be
#Then do

final_df = final_df[column_list]

答案 1 :(得分:0)

这是我目前在这种情况下的处理方式-

all_columns = df_a.values.tolist()

# Joining the dataframes
final_df = pd.merge(df_a, df_b, on='user', how='left')
# column order --> ["user", "name", "zip", "city", "gender", "country"]

# Re-arranging columns
for column in all_columns:
    if columns == "name":
        all_columns[all_columns.index(column) + 1:all_columns.index(column) + 1] = ["gender", "country"]
        break

# Assigning re-arranged columns
final_df = final_df[all_columns]
# column order --> ["user", "name", "gender", "country", "zip", "city"]

很高兴知道人们是否还有其他效率更短的方法来对该列进行重新排序。

答案 2 :(得分:0)

我会这样:

如果合并后的数据框df为100 cols。

将要放置的列存储在列表的开头。

screen B

将其余的列添加到此列表中。

first_cols =['a','z',b','c','g']

然后使用该自定义订单:

req_order = first_cols + [col for col in df.columns if col not in first_cols]

您也可以像这样重新订购:

df = df.reindex(columns = req_order)