简体   繁体   中英

DataFrame to List of OrderedDicts - how to preserve order?

when converting a DataFrame to an OrderedDict with to_dict and into I can't find a way to have it keep the order of the records. Is there a way to do this without looping through the records and converting manually?

DataFrame([{"B": 1, "A": 2}]).to_dict(orient='records', into=OrderedDict)
> [OrderedDict([('A', 2), ('B', 1)])]

Update : Cannot reproduce outside my debugger. I think that python's pprint module is throwing away the order. that's also how I first started to go down this rabbit hole - I didn't notice that my ordering problem only started after I printed my data to console. I think that VS Code python plugin is also using pprint somehow before showing debug console output, which again throws away order. I'll close this question, very sorry for the wasted time!

The key to success is to pass an initialized instance , instead of just a class.

Change your code to:

from collections import OrderedDict

dd = OrderedDict()
result = df.to_dict('records', into=dd)

Actually you can get almost the same result with defaultdict (also imported from collections ):

dd = defaultdict(list)
result = df.to_dict('records', into=dd)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM