Home > Back-end >  Getting None of [] are in the [columns] error when using isin function
Getting None of [] are in the [columns] error when using isin function

Time:07-17

I would like to get a subset of indices in a pandas data frame using a certain column. However, when I use list of values as my subset, I receive an error which seems like a very common error faced by other users. I've tried different solutions proposed in the previos posts, but none of them helped me.

import pandas as pd
import numpy as np

np.random.seed(0)
df = pd.DataFrame({'A': [5,6,3,4,5], 'B': [1,2,3,5,1]})
#I create a new column combining the first two columns
df["C"] = df['A'].astype(str)  "-"  df["B"].astype(str)

#Then, I randomly select two values from the unique values coming from column C
uniqueVal = pd.unique(df["C"])
my_subset = uniqueVal[np.random.choice(len(uniqueVal), size=2, replace=False)]

#Let's print my_subset
print(my_subset)
array(['3-3', '4-5'], dtype=object)

#Now, I would like to get all the rows containing my_subset values in column C
df[df['C']].isin(my_subset)

Here, I get the error as:

"None of [Index(['5-1', '6-2', '3-3', '4-5', '5-1'], dtype='object')] are in the [columns]"

CodePudding user response:

The grouping for when you check if df's C col is in your list is done improperly

It should be of the format df[condition]

#change the last line
print(df[df['C'].isin(my_subset)])

output

   A  B    C
2  3  3  3-3
3  4  5  4-5
  • Related