【问题标题】:Insert several columns instead of one existing to pandas dataframe [duplicate]插入几列而不是现有的一列到熊猫数据框[重复]
【发布时间】:2020-05-25 19:00:43
【问题描述】:

我有一个问题 - 如何在现有列的位置向 DataFrame 中插入多个(例如 3 个)列?换句话说,我有一列包含一些分类值,我用 one-hot 编码对其进行编码 - 结果,我获得了 3 个新列。现在,我想删除原始列并将结果列插入其位置(而不是数据框的末尾)。关于如何有效地做到这一点的任何想法?如有任何帮助,我将不胜感激。

**df1 - Original datafarme** :

   col1 col2  col3
0   4    A    0.5
1   5    B    0.78
2   6    C    0.55
3   7    A    0.78

**df2 - created one-hot encoding of categorical col2** :

   col2_A col2_B  col2_C
0   1       0       0
1   0       1       0
2   0       0       1
3   1       0       0

如何将df2的列插入df1,而不是col2来获取:

**Updated df1**

   col1 col2_A col2_b col2_C  col3
0   4    1      0        0    0.5
1   5    0      1        0    0.78
2   6    0      0        1    0.55
3   7    1      0        0    0.78

【问题讨论】:

  • 如果可能的话,单独添加df,并根据要求使用concatjoinmerge
  • df1.join(df2).drop(columns = 'col2').sort_index(axis = 1) 不是更简单吗? ?

标签: python pandas dataframe machine-learning one-hot-encoding


【解决方案1】:

使用

df_concat = pd.concat([df1, df2], axis=1)

然后删除 col_2 使用

df_concat.drop(['col_2'], axis = 1)

【讨论】:

    【解决方案2】:

    df2 中的任何新列的解决方案(不必以 col2 开头)

    Index.get_loc 用于位置,因此可以通过列前后具有DataFrame.iloc 值的位置进行过滤,然后在concat 中连接在一起,如果需要,最后删除列:

    val = 'col2'
    p = df.columns.get_loc(val)
    
    #possible solution for dummies, be free use your solution
    #df2 = pd.get_dummies(df[val])
    df = pd.concat([df.iloc[:, :p], df2, df.iloc[:, p:]], axis=1).drop(val, axis=1)
    print (df)
    
       col1  A  B  C  col3
    0     4  1  0  0  0.50
    1     5  0  1  0  0.78
    2     6  0  0  1  0.55
    3     7  1  0  0  0.78
    

    如果需要前缀:

    val = 'col2'
    p = df.columns.get_loc(val)
    #possible solution for dummies, be free use your solution
    #df2 = pd.get_dummies(df[[val]])
    df = pd.concat([df.iloc[:, :p], df2, df.iloc[:, p:]], axis=1).drop(val, axis=1)
    print (df)
    
       col1  col2_A  col2_B  col2_C  col3
    0     4       1       0       0  0.50
    1     5       0       1       0  0.78
    2     6       0       0       1  0.55
    3     7       1       0       0  0.78
    

    或在get_dummies 中使用DataFrame.pop 或其他解决方案:

    val = 'col2'
    p = df.columns.get_loc(val)
    #possible solution for dummies, be free use your solution
    #df2 = pd.get_dummies(df.pop(val))
    df = pd.concat([df.iloc[:, :p], df2, df.iloc[:, p:]], axis=1)
    print (df)
    
       col1  A  B  C  col3
    0     4  1  0  0  0.50
    1     5  0  1  0  0.78
    2     6  0  0  1  0.55
    3     7  1  0  0  0.78
    

    【讨论】:

    • 问题是How to insert columns of df2 to df1, instead of col2 to obtain:
    【解决方案3】:

    如果索引正确对齐,请使用DataFrame.joinDataFrame.drop 删除 col2DataFrame.sort_index 以排序列

    df1.join(df2).drop(columns = 'col2').sort_index(axis = 1)
    
       col1  col2_A  col2_B  col2_C  col3
    0     4       1       0       0  0.50
    1     5       0       1       0  0.78
    2     6       0       0       1  0.55
    3     7       1       0       0  0.78
    

    我们也可以使用DataFrame.pivot_table 代替pd.get_dummies

    new_df = (df1.join(df1.pivot_table(columns = 'col2',
                                       index = df1.index,
                                       aggfunc = 'size',
                                       fill_value = 0)
                          .add_prefix('col2_'))
                  .drop(columns = 'col2')
                  .sort_index(axis = 1))
    print(new_df)
       col1  col2_A  col2_B  col2_C  col3
    0     4       1       0       0  0.50
    1     5       0       1       0  0.78
    2     6       0       0       1  0.55
    3     7       1       0       0  0.78
    

    【讨论】:

    • ?????????检查输出
    • 你知道OP问题和OP DataFrame吗?我开始这么认为
    • OP 已经有 df2,我只是简单地指出,pivot_table 也适用于这种情况,但问题不是要获得 dummies
    • 我的解决方案也改了,谢谢指点。
    猜你喜欢
    • 2020-05-08
    • 2021-05-04
    • 1970-01-01
    • 2017-11-26
    • 2021-05-05
    • 2022-11-15
    • 2016-09-12
    • 2021-09-17
    • 2017-04-26
    相关资源
    最近更新 更多