area = pd.Series({'California':423967,'Texas':695662,'Nwe York':141297,'Florida':170312,'Illinois':149995})
pop = pd.Series({'California':38332521,'Texas':26448193,'Nwe York':19651127,'Florida':19552860,'Illinois':12882135})
data = pd.DataFrame({'Area':area,'Pop':pop})
data
Area
Pop
California
423967
38332521
Texas
695662
26448193
Nwe York
141297
19651127
Florida
170312
19552860
Illinois
149995
12882135
data.Pop
California 38332521
Texas 26448193
Nwe York 19651127
Florida 19552860
Illinois 12882135
Name: Pop, dtype: int64
data['Area']
California 423967
Texas 695662
Nwe York 141297
Florida 170312
Illinois 149995
Name: Area, dtype: int64
#添加新列
data['Density']= data['Pop']/ data['Area']
data
# 通过 pandas索引器 loc iloc ixprint(data.iloc[:1,:1])print(data.loc[:'Texas',:'Pop'])# 按照列名索引print(data.ix[:2,:'Pop'])#目前混合索引已经弃用
Area
California 423967
Area Pop
California 423967 38332521
Texas 695662 26448193
Area Pop
California 423967 38332521
Texas 695662 26448193
d:\python3.6\lib\site-packages\ipykernel_launcher.py:4: 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
after removing the cwd from sys.path.
# loc 的花式索引
data.loc[data.Density >100,['Pop','Density']]# 索引后更改数据
data.iloc[0,2]=90
data