Pandas:如何使用 LocIndexer?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/42940413/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Pandas: How to use LocIndexer?
提问by Liam Pieri
I am trying to figure out how to use a Locindexer object to select a subset of a dataframe.
我想弄清楚如何使用 Locindexer 对象来选择数据帧的子集。
For example
例如
var = df.loc(df['rating'] == 4)
var = df.loc(df['rating'] == 4)
Returns as
返回为
pandas.core.indexing._LocIndexer
pandas.core.indexing._LocIndexer
How do I use a LocIndexer to select the a subset of my dataframe?
如何使用 LocIndexer 选择数据帧的子集?
回答by Vaishali
You are calling it as a function. For indexing, you use [].
您将其作为函数调用。对于索引,您使用 []。
df.loc[df['rating'] == 4]
would return the row/rows in which value of column rating is 4.
将返回列评级值为 4 的行/行。
回答by Onel Harrison
You shouldn't have any problems selecting a subset using the code below.
使用下面的代码选择子集应该不会有任何问题。
subset = df.loc[(df['rating'] == 4)]
回答by 2Obe
Furthermore if you have selected the row with:
此外,如果您选择了以下行:
subset = df.loc[(df['rating'] == 4)]
you can acces a special column of this row with
您可以访问此行的特殊列
1.
1.
df.loc[(df['rating'] == 4)]["column]
2.
2.
subset["column"]