Insert Row in Python Pandas DataFrame

后端 未结 1 1410
名媛妹妹
名媛妹妹 2021-01-16 03:41

(I´m new to python, sorry for any mistakes I make, I hope you can understand me)

I have searched for a method to insert a Row into a Pandas DataFrame in Python, and

相关标签:
1条回答
  • 2021-01-16 04:30

    With a small DataFrame, if you want to insert a line, at position 1:

    df = pd.DataFrame({'a':[0,1],'b':[2,3]})
    
    df1 = pd.DataFrame([4,5]).T  
    
    pd.concat([df[:1], df1.rename(columns=dict(zip(df1.columns,df.columns))), df[1:]])
    
    #Out[46]: 
    #   a  b
    #0  0  2
    #0  4  5
    #1  1  3
    
    0 讨论(0)
提交回复
热议问题