pandas DataFrame.loc中的不同括号

时间:2018-05-24 09:53:34

标签: pandas dataframe

使用loc[x,y]loc[x][y]loc[[x]][y]的区别有何不同?乍一看,它们看起来非常相似。

df = pd.DataFrame(np.arange(6).reshape(3, 2),
                  columns=['price', 'count'],
                  index=['First', 'Second', 'Third'])
print(df)
#         price  count
# First       0      1
# Second      2      3
# Third       4      5

print(df.loc['Second', 'count'])
# 3

print(df.loc['Second']['count'])
# 3

print(df.loc[['Second'], 'count'])
# Second    3

1 个答案:

答案 0 :(得分:2)

虽然前两个在输出中是等价的,但第二个称为链式索引:

http://pandas.pydata.org/pandas-docs/stable/indexing.html#indexing-view-versus-copy

第二个类型也是Series

In[48]:
type(df.loc['Second'])

Out[48]: pandas.core.series.Series

然后索引索引值,然后返回标量值:

In[47]:
df.loc['Second']

Out[47]: 
price    2
count    3
Name: Second, dtype: int32

In[49]:
df.loc['Second']['count']

Out[49]: 3

关于最后一个,附加括号返回一个df,这就是你看到索引值而不是标量值的原因:

In[44]:
type(df.loc[['Second']])

Out[44]: pandas.core.frame.DataFrame

然后传递列,索引此df并返回匹配列,作为Series

In[46]:
type(df.loc[['Second'],'count'])

Out[46]: pandas.core.series.Series

所以它取决于你想要达到的目的,但是避免使用第二种形式,因为它可能会在尝试分配到列或df时导致意外行为

相关问题