How to work with dataframe values that are lists

Viewed 73

I have a dataframe that contains lists of values in the columns like this: enter image description here

I would like to extract all the rows that contain the value 'adhd'. code that I used:

df[df["Label"] == "adhd"]

But it returns to me an empty dataframe with just the column names. Is it because my data is in list format? is there any way to process this so that I can drop rows, view rows and work with them. I would prefer to keep them as list since many rows has multiple values like ["adhd","depression"].

5 Answers

If you want to keep them in lists you can perform operations like the ones you stated like this:

df[df['label'].apply(lambda x: x ==['adhd'])]  # keep people that only have ADHD 
df[df['label'].apply(lambda x: 'adhd' in x)]  # keep all people that have ADHD 

I would suggest however to one-hot encode your data, i.e. keep one column per label (e.g. one column for ADHD that takes the value 1 if person has ADHD, otherwise 0).

The simplest way to do this, I think is:

df['label'].apply(pd.Series)

Then you can process your data more easily.

Do this:

df[df['Label'].apply(lambda x: 'adhd' in x)]

a solution without apply method:

generated example dataframe:

example_df = pd.DataFrame(dict(data=[[str(i)] for i in range(0,10000,1000)],another_data=range(0,100000,10000)))

example_df

    data    another_data
0   [0] 0
1   [1000]  10000
2   [2000]  20000
3   [3000]  30000
4   [4000]  40000
5   [5000]  50000
6   [6000]  60000
7   [7000]  70000
8   [8000]  80000
9   [9000]  90000

fix the current dataframe:

example_df = example_df.explode('data')

>>>
    data    another_data
0   0   0
1   1000    10000
2   2000    20000
3   3000    30000
4   4000    40000
5   5000    50000
6   6000    60000
7   7000    70000
8   8000    80000
9   9000    90000

get filtered:

example_df[example_df['data'] == '1000']

>>>
    data    another_data
1   1000    10000

UPDATE

to drop the unwanted rows that have the wanted value:

example_df = example_df[example_df['data'] !='1000']
example_df 

>>>
    data    another_data
0   0   0
2   2000    20000
3   3000    30000
4   4000    40000
5   5000    50000
6   6000    60000
7   7000    70000
8   8000    80000
9   9000    90000

Hello i hope i'll help you. I discover this way a few weeks ago.

Pandas treath dictionaries, list, tuples, etc... like objects (In newer version of pandas also can be called string Dtype)

With that in mind you can do this

example_df = [*([['foo']] * 79)]
example_df = pd.DataFrame({
  'Label': [*example_df, ['Love']],
  'Post': [*example_df, ['Another Value']]
})
example_df[
  example_df.Label.str.contains('Love', regex = False)
]

This was the result

enter image description here

Also you can work with position in array with str accessor (This pandas method brings a great functions for strings Dtypes)

enter image description here

try this,

df = df[df["Label"].astype(str).str.contains("Label")]

Related