如何为熊猫数据框中的缺失索引添加新行?

我有一个看起来像这样的df

0     204,255,204
1     204,255,204
3     204,255,204
4     204,255,204
5         0,153,0
8     204,255,204
10    204,255,204

For every index that is missing from [0,1,2....,13] I want to add rows with the value 255,255,255 with the respective index

我的最终输出将如下所示:

    0     204,255,204
    1     204,255,204
    3     204,255,204
    4     204,255,204
    5         0,153,0
    8     204,255,204
    10    204,255,204
    2     255,255,255
    6     255,255,255
    7     255,255,255
    9     255,255,255
    11    255,255,255
    12    255,255,255
    13    255,255,255

有python方式可以做到这一点吗?

UPDATE The column is a categorical one

评论
  • lenim
    lenim 回复

    You can use reindex setting fill_value to the specified value:

    n = 13
    df['col'].reindex(range(n+1), fill_value= '255,255,255')
    
    0     204,255,204
    1     204,255,204
    2     255,255,255
    3     204,255,204
    4     204,255,204
    5         0,153,0
    6     255,255,255
    7     255,255,255
    8     204,255,204
    9     255,255,255
    10    204,255,204
    11    255,255,255
    12    255,255,255
    13    255,255,255
    Name: col1, dtype: object
    
  • 朕好萌
    朕好萌 回复
    1. 创建缺失的索引
    2. 创建新的df
    3. 康卡特
    add_indices = pd.Index(range(14)).difference(df.index)
    
    add_df = pd.DataFrame(index=add_indices, columns=df.columns).fillna(255)
    
    pd.concat([df, add_df])