Search code examples
pythonpandasdataframewhere-clausekeyerror

Select rows from DataFrame based on value from one of the column


DataFrame named "result"

**I am trying to get rows with the values == 0 from a DataFrame (named "result" that you see in the image attached). 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. 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)

Because the 2nd column is a binary: based on whether the restaurant review (1st column) was positive or negative (1 or 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)]

I get the error: KeyError: '0'

Can anybody please help? **


Solution

  • If you want to select rows, whose value of column '0' is 0, it should be as simple as :

    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'.