用其他列值填充NaN列,复制新行

时间:2018-06-22 17:33:36

标签: python pandas dataframe duplicates nan

我有些困惑,试图使用以下一般形式在数据集上有效完成:

id,date,ind_1,ind_2,ind_3,ind_4
1,2014-01-01,ind_1,NaN,NaN,NaN
2,2014-01-02,ind_1,NaN,ind_3,NaN
3,2014-01-03,ind_1,ind_2,ind_3,NaN

我试图弄清楚如何创建一个新列“ ind_all”,该列将填充任何非空“ ind”列。这很简单。我可以使用.idxmax()。但是,棘手的部分是我每行可以有多个“ ind”。这意味着当重复时我需要创建一个新记录。上面的示例最终应该看起来像这样:

id,date,ind_1,ind_2,ind_3,ind_4,ind_all
1,2014-01-01,ind_1,NaN,NaN,NaN,ind_1
2,2014-01-02,ind_1,NaN,ind_3,NaN,ind_1
2,2014-01-02,ind_1,NaN,ind_3,NaN,ind_3
3,2014-01-03,ind_1,ind_2,ind_3,NaN,ind_1
3,2014-01-03,ind_1,ind_2,ind_3,NaN,ind_2
3,2014-01-03,ind_1,ind_2,ind_3,NaN,ind_3

任何技巧或窍门一如既往地受到赞赏!

1 个答案:

答案 0 :(得分:4)

有一个基于merge的解决方案,它使用melt / stack来构建RHS。

v = (df.drop('date', 1)
       .melt('id')
       .drop('variable', 1)
       .dropna()
       .rename({'value' : 'ind_all'}, axis=1)
)

df.merge(v)

   id        date  ind_1  ind_2  ind_3  ind_4 ind_all
0   1  2014-01-01  ind_1    NaN    NaN    NaN   ind_1
1   2  2014-01-02  ind_1    NaN  ind_3    NaN   ind_1
2   2  2014-01-02  ind_1    NaN  ind_3    NaN   ind_3
3   3  2014-01-03  ind_1  ind_2  ind_3    NaN   ind_1
4   3  2014-01-03  ind_1  ind_2  ind_3    NaN   ind_2
5   3  2014-01-03  ind_1  ind_2  ind_3    NaN   ind_3

或者,

df.merge(df.drop('date', 1)
           .set_index('id')
           .stack()
           .reset_index(1, drop=True)
           .to_frame('ind_all'), 
         left_on='id', 
         right_index=True
)

   id        date  ind_1  ind_2  ind_3  ind_4 ind_all
0   1  2014-01-01  ind_1    NaN    NaN    NaN   ind_1
1   2  2014-01-02  ind_1    NaN  ind_3    NaN   ind_1
1   2  2014-01-02  ind_1    NaN  ind_3    NaN   ind_3
2   3  2014-01-03  ind_1  ind_2  ind_3    NaN   ind_1
2   3  2014-01-03  ind_1  ind_2  ind_3    NaN   ind_2
2   3  2014-01-03  ind_1  ind_2  ind_3    NaN   ind_3