Is there a way to store a dictionary on each row of a dataframe column using a vectorized operation?

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: Tyler Davis

||

Answers:

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: Panda Kim