6

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
wkzhu
  • 1,616
  • 13
  • 23
  • 1
    See if [this](https://stackoverflow.com/questions/40513066/append-pandas-dataframe-automatically-cast-as-float-but-want-int) solves your issue. I guess it would only work if you have a single dtype DataFrame. – ayhan Apr 13 '18 at 21:01
  • You should be able to initialize the empty dataframe as type bool. – piRSquared Apr 13 '18 at 21:38

1 Answers1

1

You can convert your dict to a DataFrame before appending to keep the data types consistent:

df = pd.DataFrame()
df.append(pd.DataFrame({'A': True, 'B': False}, index = [0]))


     A      B
0   True    False
jeremycg
  • 24,657
  • 5
  • 63
  • 74