2017-08-11 47 views
0

的子集为什么访问使用的.loc或.iloc [行] [列]一个数据帧的VS [柱] [行]顺序的语法如果我们使用方括号索引数据框,就像我们将使用列表& np.arrays一样?列&行顺序在熊猫数据帧选择/索引到数据

我肯定有一个很好的理由,我真的很好奇吧:)

E.g. 

    state total  Obama  Romney winner voters 
county              
Adams  PA 41973 35.482334 63.112001 Romney 61156 
Allegheny PA 614671 56.640219 42.185820 Obama 924351 
Armstrong PA 28322 30.696985 67.901278 Romney 42147 
Beaver  PA 80015 46.032619 52.637630 Romney 115157 
Bedford  PA 21444 22.057452 76.986570 Romney 32189 

In [5]: election.loc['Bedford']['winner'] 
Out[5]: 'Romney' 

In [6]: election['Bedford']['winner'] 
Traceback (most recent call last):................ 

In [7]: election['winner']['Bedford'] 
Out[7]: 'Romney' 

In [8]: election.loc['winner']['Bedford'] 
Out[8]: Traceback (most recent call last):................. 

回答

0

我认为最好是使用DataFrame.loc

df.loc[index_value, column_value] 

a = election.loc['Bedford', 'winner'] 
print (a) 
Romney 

如果请选择Series.locindex)或[]column)o只能得到Series

[]选择的值为columnindex - 使用1d而不使用2d数据。

#create Series from row Bedford 
print (election.loc['Bedford']) 
state   PA 
total  21444 
Obama  22.0575 
Romney 76.9866 
winner  Romney 
voters  32189 
Name: Bedford, dtype: object 

print (election.loc['Bedford']['winner']) 
Romney 

#create Series from column winner 
print (election['winner']) 
county 
Adams  Romney 
Allegheny  Obama 
Armstrong Romney 
Beaver  Romney 
Bedford  Romney 
Name: winner, dtype: object 

print (election['winner']['Bedford']) 
Romney 

和:

#no column Bedford 
print (election['Bedford']) 

KeyError: 'Bedford'

#no index value winner 
print (election.loc['winner']) 

KeyError: 'the label [winner] is not in the [index]'

+0

谢谢你,是的,我同意。现在你写了df.loc [index_value,column_value]我想这就是为什么顺序是行,列而不是列,行 - >因为行是索引,比列表/ np.array切片的一致性更重要? – Wouter

+1

对不起,以前评论不好。我认为这很难解释为什么这是通过这种方式实现的:) – jezrael