Data Mastery
Data Mastery

Reputation: 2085

Pandas - split column with dictionary into two columns with key and value

This is my column:

transcript["value"][1:4]

1    {'offer id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'}
2    {'offer id': '2906b810c7d4411798c6938adc9daaa5'}
3    {'offer id': 'fafdcd668e3743c1bb461111dcafc2a4'}

What I try to achieve is this:

       type                          offer_id
0  offer_id  0b1e1539f2cc45b7b9fa7c272da2e1d7
1  offer_id  2906b810c7d4411798c6938adc9daaa5
2  offer_id  fafdcd668e3743c1bb461111dcafc2a4

I tried to convert it into an str and then split it, this this seems error prone and actually did not work at all:

transcript["value"].str.split(":")

Does anyone know how to achieve this? Preferably something that could handle multiple dictionaries in one column?

Upvotes: 1

Views: 2127

Answers (3)

Mykola Zotko
Mykola Zotko

Reputation: 17804

You can use:

df = df['value'].apply(lambda x: pd.Series(*x.items()))
df.columns = ['type', 'offer_id']

Output:

       type                          offer_id
0  offer_id  0b1e1539f2cc45b7b9fa7c272da2e1d7
1  offer_id  2906b810c7d4411798c6938adc9daaa5
2  offer_id  fafdcd668e3743c1bb461111dcafc2a4

If keys are the same as in your case:

df['offer_id'] = df['value'].str.get('offer_id')
df['type'] = 'offer_id'

Upvotes: 0

Adit Goyal
Adit Goyal

Reputation: 36

The approach used in the previous response can be changed to be used for multiple dictionary items in a column like this:

import pandas as pd

data = [[[{'offer id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'}, {'abc': '123'}]],
       [[{'offer id': '2906b810c7d4411798c6938adc9daaa5'}, {'def': '456'}]],
       [[{'offer id': 'fafdcd668e3743c1bb461111dcafc2a4'}, {'ghi': '789'}]]]

df = pd.DataFrame(data, columns = ['Values'])
df = pd.DataFrame([df.Values[0], df.Values[1]], columns = ['dict1','dict2'])

df1 = pd.DataFrame([{'key1': key, 'value1': value } for item in df['dict1'].tolist() 
    for key, value in item.items()])

df2 = pd.DataFrame([{'key2': key, 'value2': value } for item in df['dict2'].tolist() 
    for key, value in item.items()])

pd.concat([df1,df2], axis = 1)

Output:

      key1                    value1               key2  value2
0   offer id    0b1e1539f2cc45b7b9fa7c272da2e1d7    abc   123
1   offer id    2906b810c7d4411798c6938adc9daaa5    def   456

Upvotes: 1

Dani Mesejo
Dani Mesejo

Reputation: 61910

You could do:

import pandas as pd

transcript = pd.DataFrame([
    [{'offer_id': '0b1e1539f2cc45b7b9fa7c272da2e1d7'}],
    [{'offer_id': '2906b810c7d4411798c6938adc9daaa5'}],
    [{'offer_id': 'fafdcd668e3743c1bb461111dcafc2a4'}]
], columns=['value'])


res = pd.DataFrame([{'type' : key, 'offer_id' : value } for d in transcript['value'].tolist() for key, value in d.items()])
print(res)

Output

       type                          offer_id
0  offer_id  0b1e1539f2cc45b7b9fa7c272da2e1d7
1  offer_id  2906b810c7d4411798c6938adc9daaa5
2  offer_id  fafdcd668e3743c1bb461111dcafc2a4

Upvotes: 2

Related Questions