简体   繁体   English

根据一列中的值从DataFrame中选择行

[英]Select rows from DataFrame based on value from one of the column

DataFrame named "result" DataFrame命名为“结果”

**I am trying to get rows with the values == 0 from a DataFrame (named " result " that you see in the image attached). **我正在尝试从DataFrame中获取values == 0的行(您在所附图像中看到的名为“ result ”)。 The SQL Query for something like this will be like: SELECT * FROM RESULT WHERE column_name equal to some_value My DataFrame has 2 columns. 对于类似这样的SQL查询,将类似于: SELECT * FROM RESULT WHERE column_name等于some_value我的DataFrame有2列。 the 1st one is i think named ' Index ' and the 2nd one is named ' 0 ' (the names are by default -- didn't bother to change) 我认为第一个命名为“ Index ”,第二个命名为“ 0 ”(默认情况下名称为-无需更改)

Because the 2nd column is a binary: based on whether the restaurant review (1st column) was positive or negative (1 or 0). 因为第二列是二进制:基于餐厅评论(第一列)是肯定的还是否定的(1或0)。 I want to get only the rows (of the two columns based on the negative reviews). 我只想获取(基于负面评论的两列中的行)。 when I try the code: df.loc[df['column_name'].isin(some_values)] or in my case result.loc[result['0'].isin(0)] 当我尝试代码时: df.loc[df['column_name'].isin(some_values)]或在我的情况下为df.loc[df['column_name'].isin(some_values)] result.loc[result['0'].isin(0)]

I get the error: KeyError: '0' 我收到错误: KeyError: '0'

Can anybody please help? 有人可以帮忙吗? ** **

If you want to select rows, whose value of column '0' is 0, it should be as simple as : 如果要选择“ 0”列的值为0的行,它应该像这样简单:

result[result[0] == 0]

However, as sacul has pointed out, you should check whether the name of your column is of type 'str' or 'int'. 但是,正如sacul指出的那样,您应该检查列的名称是'str'还是'int'类型。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 基于列值的 DataFrame 中的 select 行,限制为 16384 行 - select rows from a DataFrame based on column value, limit to 16384 rows 基于列值的 DataFrame 中的 select 行? - select rows from a DataFrame based on column values? 根据另一个数据框中的值从DataFrame中选择行,并根据第二个DataFrame使用值更新其中一个列 - Select rows from a DataFrame based on a values in another dataframe and updating one of the column with values according to the second DataFrame Pandas数据框根据查询数据框中的值选择行,然后根据列值选择其他条件 - Pandas Dataframe Select rows based on values from a lookup dataframe and then another condition based on column value 根据汇总值从数据框中选择行 - select rows from Dataframe based on aggregated value 从 Pandas DataFrame 中选择一列中具有相同值但另一列中具有不同值的行 - Select rows from a Pandas DataFrame with same values in one column but different value in the other column 根据特定列或列中是否存在空值,从DataFrame中选择行 - Select rows from a DataFrame based on presence of null value in specific column or columns Pandas - 来自 dataframe 的 Select 行基于列中的列表 - Pandas - Select rows from a dataframe based on list in a column 在pandas的列中选择基于True或False的DataFrame中的行 - Select rows from a DataFrame based on True or False in a column in pandas 根据其他列的唯一值从数据框中选择行? - Select rows from dataframe based on a unique values of other column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM