Reputation: 631
I am trying to convert a series of dictionaries into a dataframe
0 {'neg': 0.0, 'neu': 0.462, 'pos': 0.538}
1 {'neg': 0.0, 'neu': 0.609, 'pos': 0.391}
2 {'neg': 0.043, 'neu': 0.772, 'pos': 0.185}
3 {'neg': 0.035, 'neu': 0.765, 'pos': 0.2}
4 {'neg': 0.0, 'neu': 0.655, 'pos': 0.345}
5 {'neg': 0.0, 'neu': 0.631, 'pos': 0.369}
I want the resulting DataFrame to have each key be its own column.
neg neu pos
0.0. 0.462 0.538
0.0 0.609 0.391
.. .. ..
How can I accomplish this with Pandas?
Upvotes: 11
Views: 7812
Reputation:
Given your Series, ser
ser
Out:
0 {'neg': 0.0, 'neu': 0.462, 'pos': 0.538}
1 {'neg': 0.0, 'neu': 0.609, 'pos': 0.391}
2 {'neg': 0.043, 'neu': 0.772, 'pos': 0.185}
3 {'neg': 0.035, 'neu': 0.765, 'pos': 0.2}
4 {'neg': 0.0, 'neu': 0.655, 'pos': 0.345}
5 {'neg': 0.0, 'neu': 0.631, 'pos': 0.369}
You can convert the Series to a list and call the DataFrame constructor:
pd.DataFrame(ser.tolist())
Out:
neg neu pos
0 0.000 0.462 0.538
1 0.000 0.609 0.391
2 0.043 0.772 0.185
3 0.035 0.765 0.200
4 0.000 0.655 0.345
5 0.000 0.631 0.369
Or you can apply
the pd.Series constructor to each row. apply
will be flexible and return a DataFrame since each row is a Series now.
ser.apply(pd.Series)
Out:
neg neu pos
0 0.000 0.462 0.538
1 0.000 0.609 0.391
2 0.043 0.772 0.185
3 0.035 0.765 0.200
4 0.000 0.655 0.345
5 0.000 0.631 0.369
Upvotes: 32
Reputation: 3464
There is probably a better way to do this... but this appears pretty easy with the structured data you have.
Otherwise look at this post for reforming the dictionary
import pandas as pd
a = [{'neg': 0.0, 'neu': 0.462, 'pos': 0.538},
{'neg': 0.0, 'neu': 0.609, 'pos': 0.391},
{'neg': 0.043, 'neu': 0.772, 'pos': 0.185},
{'neg': 0.035, 'neu': 0.765, 'pos': 0.2},
{'neg': 0.0, 'neu': 0.655, 'pos': 0.345},
{'neg': 0.0, 'neu': 0.631, 'pos': 0.369}]
b = dict()
for key in a[0].keys():
b[key] = []
for dic in a:
b[key].append(dic[key])
pd.DataFrame(b)
Upvotes: 0