I have a pandas Series of dictionnaries, and I want to convert it to a data frame with the same index.
The only way I found is to pass through the to_dict
method of the series, which is not very efficient because it goes back to pure python mode instead of numpy/pandas/cython.
Do you have suggestions for a better approach?
Thanks a lot.
>>> import pandas as pd
>>> flagInfoSeries = pd.Series(({'a': 1, 'b': 2}, {'a': 10, 'b': 20}))
>>> flagInfoSeries
0 {'a': 1, 'b': 2}
1 {'a': 10, 'b': 20}
dtype: object
>>> pd.DataFrame(flagInfoSeries.to_dict()).T
a b
0 1 2
1 10 20
I think you can use comprehension:
Timing:
EDIT:
If you need keep index, try add
index=flagInfoSeries.index
toDataFrame
constructor:Timings:
Sample:
This avoids
to_dict
, butapply
could be slow too:Edit: I see that jezrael has added timing comparisons. Here is mine: