Matina G
Matina G

Reputation: 1582

pandas.DataFrame.to_dict behavior

I am trying to transform my dataframe to a dict , in order to use those dicts to instanciate some class objects. Following documentation, http://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.to_dict.html, the rule I need in my case is orient='records'. However, I noticed that it does not behave as I expected when column names are integers,or strings containing only integer:

import pandas as pd
df = pd.DataFrame({'a':[1,2,3], 15:[4,5,6], '302':[5,6,7]})
mydict = df.to_dict(orient='records')
print(mydict)

returns

[{'_2':4, 'a':1, '_0':5},{'_2':5, 'a':2, '_0':6}, {'_2':6, 'a':3, '_0':7}]

I found a way to work around the problem, doing:

mydict = list(df.to_dict(orient='index').values()) 

but I am at loss as to why this is happening. I am on python 3.5 and on pandas 0.24. Any ideas?

Upvotes: 2

Views: 356

Answers (1)

jakevdp
jakevdp

Reputation: 86513

This is a bug that was introduced in pandas 0.24.0, and fixed in 0.24.1. See https://github.com/pandas-dev/pandas/issues/24940

Upvotes: 2

Related Questions