Jack
Jack

Reputation: 1754

get dict from dataframe which rows contains many values?

Input

df 

A         B
a         23
b,c       34
d,e,%f    30

Goal

df_dct = {'a':23,'b':34,'c':34,'d':'30','e':'30','f':30}

The details as below:

Try

Upvotes: 1

Views: 39

Answers (2)

Mayank Porwal
Mayank Porwal

Reputation: 34086

You can use df.explode() for pandas >= 0.25 with df.to_dict():

In [32]: df.A = df.A.str.replace("%", "")
In [42]: df_dct = df.assign(var1=df['A'].str.split(',')).explode('var1').drop('A', 1).set_index('var1').to_dict()['B'] 

In [43]: df_dct
Out[43]: {'a': 23, 'b': 34, 'c': 34, 'd': 30, 'e': 30, 'f': 30}

Upvotes: 1

sammywemmy
sammywemmy

Reputation: 28709

Remove the percentage from column A with str replace

df["A"] = df.A.str.replace("%", "")

Use itertools' product to get the pairing of each element in A and B for each row, then combine them into one list, using chain

from itertools import product, chain
#apply dict to get your final result
dict(chain.from_iterable((product(A.split(","),[B])) for A,B in df.to_numpy()))

{'a': 23, 'b': 34, 'c': 34, 'd': 30, 'e': 30, 'f': 30}

Upvotes: 0

Related Questions