I am finding that the output of the pandas dataframe classes to_dict method is different for columns of the same dtype in different dataframes. I have one dataframe where all columns are of type int64. to_dict on this dataframe will print values of type int64. In another dataframe where there is a column of type int64 and another column of a different type, the int64 column values, after to_dict, are represented as type int. Is there an explanation why this method should be producing a representation of a value that is dependent on types of other values w/in the same dataframe?
import pandas as pd
df1 = pd.DataFrame([{'a': 1, 'b': 'str'}])
df2 = pd.DataFrame([{'a': 1, 'b': 2}])
l1 = df1.to_dict(orient='records')
l2 = df2.to_dict(orient='records')
for i in l1:
for k in i:
print(type(i[k]))
for i in l2:
for k in i:
print(type(i[k]))
output:
<class 'int'>
<class 'str'>
<class 'numpy.int64'>
<class 'numpy.int64'>
Ideally, I would like some consistency. Whatever that may be. Thanks in advance for any insight!