接上一章
可以直接对列进行赋值,比如赋一个常量:
In [97]: frame2['dept']
Out[97]:
one NaN
two NaN
three NaN
four NaN
five NaN
six NaN
Name: dept, dtype: object
In [98]: frame2['dept']=16.5
In [99]: frame2
Out[99]:
year state pop dept
one 2000 Ohio 1.5 16.5
two 2001 Ohio 1.7 16.5
three 2002 Ohio 3.6 16.5
four 2001 Nevada 2.4 16.5
five 2002 Nevada 2.9 16.5
six 2003 Nevada 3.2 16.5
也可以给一个列赋值一个数组,但是如果是数组的话 ,长度应该跟DataFrame的行数相等。
In [100]: frame2['dept']=np.arange(6)
In [101]: frame2
Out[101]:
year state pop dept
one 2000 Ohio 1.5 0
two 2001 Ohio 1.7 1
three 2002 Ohio 3.6 2
four 2001 Nevada 2.4 3
five 2002 Nevada 2.9 4
six 2003 Nevada 3.2 5
也可以把一个Series赋值给一列(当然是可以的,因为列本来就是Series),这种赋值时,会根据索引自动对齐。
In [107]: val=pd.Series([-1.2, -1.5, -1.7,1.9],index=['two','four','five','seven'])
In [108]: frame2['dept']=val
In [109]: frame2
Out[109]:
year state pop dept
one 2000 Ohio 1.5 NaN
two 2001 Ohio 1.7 -1.2
three 2002 Ohio 3.6 NaN
four 2001 Nevada 2.4 -1.5
five 2002 Nevada 2.9 -1.7
six 2003 Nevada 3.2 NaN
如果被赋值的列不存在,则会生成一个新的列。
In [110]: frame2['eastern'] = frame2.state == 'Ohio'
In [111]: frame2
Out[111]:
year state pop dept eastern
one 2000 Ohio 1.5 NaN True
two 2001 Ohio 1.7 -1.2 True
three 2002 Ohio 3.6 NaN True
four 2001 Nevada 2.4 -1.5 False
five 2002 Nevada 2.9 -1.7 False
six 2003 Nevada 3.2 NaN False
如果要删除列,可以用跟字典一样的的方法 del
In [112]: del frame2['eastern']
In [113]: frame2.columns
Out[113]: Index(['year', 'state', 'pop', 'dept'], dtype='object')