pandas行与列选择

>>>import pandas as pd

>>>d = {'one' : pd.Series([1, 2, 3], index=['a', 'b', 'c']), 
     'two' : pd.Series([1, 2, 3, 4], index=['a', 'b', 'c', 'd'])}

>>>df = pd.DataFrame(d)
>>>print(df)
      one    two
a     1.0     1
b     2.0     2
c     3.0     3
d     NaN     4

列选择:
>>>df['one']  等价于 使用标签索引:df.loc[:,'one']  等价于  使用位置索引:df.iloc[:,0]
a     1.0     
b     2.0    
c     3.0     
d     NaN 
   
行选择:
>>>使用标签索引:df.loc['a',:]  等价于  使用位置索引:df.iloc[0,:]   
one    1.0
two    1.0
>>>df['a']  这种方法默认是按列的,如果找不到列为 'a',就显示为错
KeyError: 'a'

猜你喜欢

转载自blog.csdn.net/xiaohuihui1994/article/details/84858224
今日推荐