Adding new column to existing DataFrame in Python pandas

后端 未结 25 1104
你的背包
你的背包 2020-11-22 01:15

I have the following indexed DataFrame with named columns and rows not- continuous numbers:

          a         b         c         d
2  0.671399  0.101208 -         


        
25条回答
  •  南笙
    南笙 (楼主)
    2020-11-22 01:29

    Before assigning a new column, if you have indexed data, you need to sort the index. At least in my case I had to:

    data.set_index(['index_column'], inplace=True)
    "if index is unsorted, assignment of a new column will fail"        
    data.sort_index(inplace = True)
    data.loc['index_value1', 'column_y'] = np.random.randn(data.loc['index_value1', 'column_x'].shape[0])
    

提交回复
热议问题