Add lists inside dictionary to DataFrame as new columns

Viewed 1264

Let's say I have the following pandas DataFrame:

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

And the following dictionary:

d = {'a': [10, 10, 10], 'b': [100, 100, 100]}

What's the best way to add the dictionary to the DataFrame to get the following:

     x   y   z    a    b
 0   0   3   6   10  100
 1   1   4   7   10  100
 2   2   5   8   10  100

This is what I've come up with so far but I feel like there has to be a better way:

df_bigger = pd.concat([df, pd.DataFrame(d)], axis=1)
4 Answers
Related