-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
order not preserved when constructing DataFrame from a list of OrderedDicts #13304
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
pls post a copy-pastable reproducible example at the top of the PR. yes this could be an enhancement. pull-requests are welcome. |
Here's one: >>> from pandas.compat import OrderedDict
>>> from pandas import DataFrame
>>>
>>> data = OrderedDict()
>>> data['c'] = 2
>>> data['b'] = 1
>>>
>>> data
OrderedDict([('c', 2), ('b', 1)])
>>>
>>> DataFrame([data])
b c
0 1 2 |
I should point out though that if you pass in the >>> from pandas.compat import OrderedDict
>>> from pandas import DataFrame
>>>
>>> data = OrderedDict()
>>> data['c'] = [2]
>>> data['b'] = [1]
>>>
>>> data
OrderedDict([('c', [2]), ('b', [1])])
>>>
>>> DataFrame(data)
c b
0 2 1 |
The "bug" traces back to the initialization of arrays from the list of dictionaries. The initialization sorts the keys at the end, so we just need a boolean to tell it not to sort when we have |
issue is explained here:
http://stackoverflow.com/questions/37484738/pandas-dataframe-construction-from-a-list-of-ordereddict-preserving-columns-ord?noredirect=1#comment62465925_37484738
workaround with constructor option "columns" works but not elegant.
The text was updated successfully, but these errors were encountered: