Say I create an empty dataframe:
df = pd.DataFrame()
and I add a dict via df.append()
:
df.append({'A': 'foo', 'B': 'bar'}, ignore_index=True)
This gives me the intended result of
A B
0 foo bar
However, if there are any booleans in the dict values, i.e.,
df.append({'A': True, 'B': False}, ignore_index=True)
The booleans are converted into floats.
A B
0 1.0 0.0
Why this is happening / how can I prevent this conversion? I'd prefer not do anything to the finished dataframe if possible (i.e., prefer not to coerce from float back to boolean).
EDIT: found my own solution, but still would like to know why the above behavior is happening. My solution is:
df.append(pd.DataFrame.from_dict({'A': True, 'B': False}, orient='index').T, ignore_index=True)
Which gives the desired
A B
0 True False