Python 如何在 Pandas 的数据框中获取行号?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43193880/
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
How to get row number in dataframe in Pandas?
提问by sprogissd
How can I get the number of the row in a dataframe that contains a certain value in a certain column using Pandas? For example, I have the following dataframe:
如何使用 Pandas 获取包含特定列中特定值的数据框中的行数?例如,我有以下数据框:
ClientID LastName
0 34 Johnson
1 67 Smith
2 53 Brows
How can I find the number of the row that has 'Smith' in 'LastName' column?
如何找到“LastName”列中包含“Smith”的行号?
回答by joelostblom
To get all indices that matches 'Smith'
获取与“史密斯”匹配的所有索引
>>> df[df['LastName'] == 'Smith'].index
Int64Index([1], dtype='int64')
or as a numpy array
或者作为一个 numpy 数组
>>> df[df['LastName'] == 'Smith'].index.to_numpy() # .values on older versions
array([1])
or if there is only one and you want the integer, you can subset
或者如果只有一个并且你想要整数,你可以子集
>>> df[df['LastName'] == 'Smith'].index[0]
1
You could use the same boolean expressions with .loc
, but it is not needed unless you also want to select a certain column, which is redundant when you only want the row number/index.
您可以使用与 相同的布尔表达式.loc
,但除非您还想选择某个列,否则不需要它,这在您只需要行号/索引时是多余的。
回答by piRSquared
df.index[df.LastName == 'Smith']
Or
或者
df.query('LastName == "Smith"').index
Will return all row indices where LastName
is Smith
将返回所有行索引,其中LastName
为Smith
Int64Index([1], dtype='int64')
回答by Vaishali
df.loc[df.LastName == 'Smith']
will return the row
将返回行
ClientID LastName
1 67 Smith
and
和
df.loc[df.LastName == 'Smith'].index
will return the index
将返回索引
Int64Index([1], dtype='int64')
NOTE: Column names 'LastName' and 'Last Name' or even 'lastname' are three unique names. The best practice would be to first check the exact name using df.columns. If you really need to strip the column names of all the white spaces, you can first do
注意:列名称“LastName”和“Last Name”甚至“lastname”是三个唯一名称。最佳做法是首先使用 df.columns 检查确切的名称。如果你真的需要去掉所有空格的列名,你可以先做
df.columns = [x.strip().replace(' ', '') for x in df.columns]
回答by Veera Samantula
len(df[df["Lastname"]=="Smith"].values)
回答by Scott Boston
count_smiths = (df['LastName'] == 'Smith').sum()
回答by Sameer
You can simply use shape method
df[df['LastName'] == 'Smith'].shape
您可以简单地使用形状方法
df[df['LastName'] == 'Smith'].shape
Output(1,1)
输出(1,1)
Which indicates 1 row and 1 column. This way you can get the idea of whole datasets
这表示 1 行和 1 列。通过这种方式,您可以了解整个数据集
Let me explain the above code
DataframeName[DataframeName['Column_name'] == 'Value to match in column']
让我解释一下上面的代码
DataframeName[DataframeName['Column_name'] == 'Value to match in column']