Adding new column to existing DataFrame in Python pandas

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

    The following is what I did... But I'm pretty new to pandas and really Python in general, so no promises.

    df = pd.DataFrame([[1, 2], [3, 4], [5,6]], columns=list('AB'))
    
    newCol = [3,5,7]
    newName = 'C'
    
    values = np.insert(df.values,df.shape[1],newCol,axis=1)
    header = df.columns.values.tolist()
    header.append(newName)
    
    df = pd.DataFrame(values,columns=header)
    

提交回复
热议问题