1. 程式人生 > >如何在DataFrame 中優雅的增加一行,一列

如何在DataFrame 中優雅的增加一行,一列

<font color=‘darkgreen’,size=4.5>    優雅的增加一行,一定要優雅!

df=DataFrame(np.arange(16).reshape((4,4)),index=['a','b','c','d'],columns=['one','two','three','four'])  
df.loc['new_raw'] = '3'

df
Out[84]: 
        one two three four
a         0   1     2    3
b         4   5     6    7
c         8   9    10   11
d        12  13    14   15
new_raw   3   3     3    3

<font color=‘green’,size=4.5>    優雅的增加一列,一定要優雅!

df['new_colu']='12'#向 DataFrame 新增一列,該列為同一值

df
Out[93]: 
        one two three four new_colu
a         0   1     2    3       12
b         4   5     6    7       12
c         8   9    10   11       12
d        12  13    14   15       12
new_raw   3   3     3    3       12