Python 3.6.7 | packaged by conda-forge | (default, Nov 21 2018, 03:09:43)
Type 'copyright', 'credits' or 'license' for more information
IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help.
In [1]: import pandas as pd
In [2]: df = pd.DataFrame({('A_%d' % i): [i] for i in range(5)})
In [3]: df
Out[3]:
A_0 A_1 A_2 A_3 A_4
0 0 1 2 3 4
In [4]: df.to_dict('records')
Out[4]: [{'A_0': 0, 'A_1': 1, 'A_2': 2, 'A_3': 3, 'A_4': 4}]
In [5]: df_bad = pd.DataFrame({str(i): [i] for i in range(5)})
In [6]: df_bad
Out[6]:
0 1 2 3 4
0 0 1 2 3 4
In [7]: df_bad.to_dict('records')
Out[7]: [{'_0': 0, '_1': 1, '_2': 2, '_3': 3, '_4': 4}]
Code Sample, a copy-pastable example if possible
Python 3.6.7 | packaged by conda-forge | (default, Nov 21 2018, 03:09:43) Type 'copyright', 'credits' or 'license' for more information IPython 7.2.0 -- An enhanced Interactive Python. Type '?' for help. In [1]: import pandas as pd In [2]: df = pd.DataFrame({('A_%d' % i): [i] for i in range(5)}) In [3]: df Out[3]: A_0 A_1 A_2 A_3 A_4 0 0 1 2 3 4 In [4]: df.to_dict('records') Out[4]: [{'A_0': 0, 'A_1': 1, 'A_2': 2, 'A_3': 3, 'A_4': 4}] In [5]: df_bad = pd.DataFrame({str(i): [i] for i in range(5)}) In [6]: df_bad Out[6]: 0 1 2 3 4 0 0 1 2 3 4 In [7]: df_bad.to_dict('records') Out[7]: [{'_0': 0, '_1': 1, '_2': 2, '_3': 3, '_4': 4}]Problem description
While looking into #24939 I noticed that an
_is added before the column name in the output ofto_dict('records')when the column names are integer-strings. I believe this behavior is new with 0.24.0, but I'm not certain.Conda info
Details
Output of
pd.show_versions()Details