Adding new column to existing DataFrame in Python pandas

后端 未结 25 1277
你的背包
你的背包 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:31

    to insert a new column at a given location (0 <= loc <= amount of columns) in a data frame, just use Dataframe.insert:

    DataFrame.insert(loc, column, value)
    

    Therefore, if you want to add the column e at the end of a data frame called df, you can use:

    e = [-0.335485, -1.166658, -0.385571]    
    DataFrame.insert(loc=len(df.columns), column='e', value=e)
    

    value can be a Series, an integer (in which case all cells get filled with this one value), or an array-like structure

    https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.insert.html

提交回复
热议问题