goutham_mi3
goutham_mi3

Reputation: 345

How to filter the values in a dataframe where a column having list of strings as value?

        domain          intents  expressionId                     name
0           []               []             8  When I dare to be powerful – to use my strengt...
1           []               []             7  When one door of happiness closes, another ope...
2           []               []             6  The first step toward success is taken when yo...
3           []               []             5                  Get busy living or get busy dying
4  [financial, education]    [resolutions]  4  You know you’re in love when you can’t fall as...
5  [financial, business]     [materialistic]3                         Honesty is the best policy

Here is my dataframe which is having the domain column with list of strings. And, what I want is to fetch only rows that are having 'domain' contains 'financial'(domain==financial), so that I get the below results:

        domain          intents  expressionId                     name
4  [financial, education]    [resolutions]  4  You know you’re in love when you can’t fall as...
5  [financial, business]     [materialistic]3                         Honesty is the best policy

What I have tried so far is with the below command:

df['domain'].map(lambda x: 'financial' in x)

This is returning, the column with objectType as 'bool'.

0    False
1    False
2    False
3    False
4     True
5     True
Name: domain, dtype: bool

But what I want is the filtered result not the bool values.

Please help me with this. Thank you.

Upvotes: 1

Views: 55

Answers (3)

Kyle L
Kyle L

Reputation: 11

Without actually trying to make your dataframe, I would suggest:

new_df = old_df[old_df['domain'] == 'financial']

Upvotes: 0

frankr6591
frankr6591

Reputation: 1247

df[df.domain.apply(lambda v: 'financial' in v)]

df.domain.contains('financial') didn't work for me.

Upvotes: 1

Paul Brennan
Paul Brennan

Reputation: 2696

dfFinaicals = df[df['domain'].map(lambda x: 'financial' in x)] 

This is just asking the domain and using it to select the rows. You were almost there.

dfFinaicals = df[df['domain'].contains('financial')]

is more elegant

Upvotes: 1

Related Questions