rnv86
rnv86

Reputation: 880

Iterate over a column list pandas with a dictionary

I have a list in a pandas dataframe:

0: [car, telephone]
1: [computer, beach, book, language]
2: [rice, bus, street]

Every list is in each row.Also, this list has different length in every row. and I have a dictionary:

dict = {'car': 'transport',
'rice':'food'
'book':'reading'
}

After that I have flattened the dict

d = {val:key for key, lst in dict.items() for val in lst}

I would like to iterate over the all items in the list and create a column of this kind,

this is the desired output:

index col1  col2
    0: [car, telephone],transport
    1: [computer, beach, book, language], reading
    2: [rice, bus, street], food

I have tried:

  df['col2'] = data_df['col1'].index.map(d)

but I get

col2
NaN
NaN
NaN 

Upvotes: 1

Views: 1015

Answers (2)

RJ Adriaansen
RJ Adriaansen

Reputation: 9619

You can use apply on a custom function:

import pandas as pd

df = pd.DataFrame([{'col1': ['car', 'telephone']}, {'col1': ['computer', 'beach', 'book', 'language']}, {'col1': ['rice', 'bus', 'street']}])


def get_col2(lst):
    d={'car': 'transport','rice':'food','book':'reading'}
    for k,v in d.items():
        if k in lst:
            return v
        
df['col2'] = df['col1'].apply(get_col2)

Output:

col1 col2
0 ['car', 'telephone'] transport
1 ['computer', 'beach', 'book', 'language'] reading
2 ['rice', 'bus', 'street'] food

Upvotes: 1

Andreas
Andreas

Reputation: 9197

You can .explode then use the dictionary for translation and then group again:

Sample data:

import pandas as pd
data = {'id': {0: 1, 1: 2, 2: 3}, 'col': {0: ['car', 'telephone'], 1: ['computer', 'beach', 'book', 'language'], 2: ['rice', 'bus', 'street']}}
df = pd.DataFrame(data)

dct = {'car': 'transport', 'rice':'food', 'book':'reading'}

Code:

df2 = df.explode('col')
df2['col2'] = df2['col'].replace(dct)
df['col2'] = df2[~df2['col'].eq(df2['col2'])]['col2']

Output:

   id                                col       col2
0   1                   [car, telephone]  transport
1   2  [computer, beach, book, language]    reading
2   3                [rice, bus, street]       food

Upvotes: 1

Related Questions