D:\anacoda\lib\site-packages\ipykernel_launcher.py:1: DeprecationWarning:
.ix is deprecated. Please use
.loc for label based indexing or
.iloc for positional indexing
See the documentation here:
http://pandas.pydata.org/pandas-docs/stable/indexing.html#ix-indexer-is-deprecated
"""Entry point for launching an IPython kernel.
year 2002
state Ohio
pop 3.6
dept NaN
Name: thress, dtype: object
frame2.loc['two',:]
year 2001
state Ohio
pop 1.7
dept NaN
Name: two, dtype: object
frame2.iloc[2]
year 2002
state Ohio
pop 3.6
dept NaN
Name: thress, dtype: object
列可以通过赋值的方式进行修改,但是长度需要和DataFrame匹配,
frame2['dept']=15
frame2
year
state
pop
dept
Digit
one
2000
Ohio
1.5
15
two
2001
Ohio
1.7
15
thress
2002
Ohio
3.6
15
four
2001
Nevada
2.4
15
five
2002
Nevada
2.9
15
frame2.dept = np.arange(5.)
frame2
year
state
pop
dept
Digit
one
2000
Ohio
1.5
0.0
two
2001
Ohio
1.7
1.0
thress
2002
Ohio
3.6
2.0
four
2001
Nevada
2.4
3.0
five
2002
Nevada
2.9
4.0
# 如果赋值是一个Series对象,则会精确匹配到DataFrame对象的索引,所有空位都会补全缺失值
val = Series([-1.2,-1.5,-1.7,-1.3], index =['two','thress','five','T'])
frame2.dept = val
frame2
year
state
pop
dept
Digit
one
2000
Ohio
1.5
NaN
two
2001
Ohio
1.7
-1.2
thress
2002
Ohio
3.6
-1.5
four
2001
Nevada
2.4
NaN
five
2002
Nevada
2.9
-1.7
创建新的列,为不存在的列赋值会创建新的列,关键字 del 用于删除列
frame2['es']= frame2.state =='Ohio'
frame2
year
state
pop
dept
es
Digit
one
2000
Ohio
1.5
NaN
True
two
2001
Ohio
1.7
-1.2
True
thress
2002
Ohio
3.6
-1.5
True
four
2001
Nevada
2.4
NaN
False
five
2002
Nevada
2.9
-1.7
False
del frame2['es']
frame2
year
state
pop
dept
Digit
one
2000
Ohio
1.5
NaN
two
2001
Ohio
1.7
-1.2
thress
2002
Ohio
3.6
-1.5
four
2001
Nevada
2.4
NaN
five
2002
Nevada
2.9
-1.7
通过嵌套字典创建 DataFrame对象: 外层键为列索引,内层键为行索引
pop ={'Nevada':{2001:2.4,2002:2.9},'Ohio':{2000:1.2,2001:1.7,2002:3.6}}