store a dictionary on each row of a dataframe

Question:

I am attempting to next a dictionary inside of a dataframe.

here’s an example of what I have:

x y z
1 2 3
4 5 6
7 8 9

here’s an example of what I want:

x y z
1 2 {'z':3}
4 5 {'z':6}
7 8 {'z':9}

For this specific application, the whole point of using pandas is the vectorized operations that are scalable and efficient. Is it possible to transform that column into a column of dictionaries? I have attempted to use string concatenation, but then it is stored in pandas as a string and not a dict, and returns later with quotations around the dictionary because it is a string.

Asked By: javad

||

Answers:

This is not possible to vectorize, the best approach here is to use a list comprehension:

df['z'] = [{'z': z} for z in df['z']]

Alternatively, using to_dict:

df['z'] = df[['z']].to_dict('records')

Output:

   x  y         z
0  1  2  {'z': 3}
1  4  5  {'z': 6}
2  7  8  {'z': 9}

timings

On 300k rows

# list comprehension
102 ms ± 9.37 ms per loop (mean ± std. dev. of 7 runs, 10 loops each)

# to_dict
585 ms ± 25.6 ms per loop (mean ± std. dev. of 7 runs, 1 loop each)
Answered By: mozway

Example

data = {'x': {0: 1, 1: 4, 2: 7}, 'y': {0: 2, 1: 5, 2: 8}, 'z': {0: 3, 1: 6, 2: 9}}
df = pd.DataFrame(data)

Code

df['z'] = pd.Series(df[['z']].T.to_dict())

df

    x   y   z
0   1   2   {'z': 3}
1   4   5   {'z': 6}
2   7   8   {'z': 9}
Answered By: ali bakhtiari
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.