Reputation: 1325
I have a python dictionary below:
dict = {'stock1': (5,6,7), 'stock2': (1,2,3),'stock3': (7,8,9)};
I want to change dictionary to dataframe like:
How to write the code?
I use:
pd.DataFrame(list(dict.iteritems()),columns=['name','closePrice'])
But it will get wrong. Could anyone help?
Upvotes: 1
Views: 402
Reputation: 25699
Try this: Do not name your variables python key words!
dd = {'stock1': (5,6,7), 'stock2': (1,2,3),'stock3': (7,8,9)};
#pd.DataFrame(dd) Should work!
pd.DataFrame.from_dict(dd, orient='columns')
stock1 stock2 stock3
0 5 1 7
1 6 2 8
2 7 3 9
Upvotes: 2
Reputation: 474201
You are overcomplicating the problem, just pass your dictionary into the DataFrame
constructor:
import pandas as pd
d = {'stock1': (5,6,7), 'stock2': (1,2,3),'stock3': (7,8,9)}
print(pd.DataFrame(d))
Prints:
stock1 stock2 stock3
0 5 1 7
1 6 2 8
2 7 3 9
Upvotes: 4