Silver Duck
Silver Duck

Reputation: 640

Creating dataframe rows from keys of underlying dict

I have the following dataframe:

'A'  'B'  'Dict'
 a    f    {'k1': 'v1', 'k2': 'v2'}
 b    h    {}
 c    g    {'k3': 'v3'}
 …    …    …

And I would like the following:

'A'  'B'  'Keys'
 a    f    k1
 a    f    k2
 c    g    k3
 …    …    …

That is, getting the keys of a dict to make the rows of the new dataframe. The dict may be empty or contain arbitrary number of elements.

Here is the solution I am using now. It works, but seems to be quite inefficient and not very pythonic…

my_list = []

for row in subset.iterrows():
  for key in row[1][2].keys():
    my_list.append((row[1][0], row[1][1], key))

new_df = pd.DataFrame(my_list)

Thanks in advance for your ideas!

Upvotes: 1

Views: 43

Answers (2)

BENY
BENY

Reputation: 323366

Or you can set_index()

df.set_index(['A','B'])['Dict'].apply(pd.Series).stack().reset_index()

Upvotes: 3

cs95
cs95

Reputation: 402922

You'll need stack here:

pd.DataFrame(
   df.Dict.tolist(), 
   index=pd.MultiIndex.from_arrays([df.A, df.B])
).stack().reset_index()

   A  B level_2   0
0  a  f      k1  v1
1  a  f      k2  v2
2  c  g      k3  v3

Upvotes: 1

Related Questions