1

Can you help me fix this code? I'm trying to create a new dataframe with the lines I want.

import pandas as pd

#Creating dataframe
d = {'col1': [1,2,3,4,5,6,7,8,9,10], 'col2': ['azs','bdq','bzm','bqm','csm','dqs','cm','a','z','c']}
df = pd.DataFrame(data=d)

x = df[df['col1'].int.contains("2|3|5",na=False)] 

1 Answers1

1

For your final line, you want:

x = df[df['col1'].isin({2,3,5})]

You've modeled your membership test off of the pd.Series.str.contains method. The way to do a membership test for an int is to use the pd.Series.isin method, as shown above. Using a set (i.e. {}) is preferable for membership tests.

captnsupremo
  • 689
  • 5
  • 11