Skip to content
Advertisement

Stop Pandas from converting int to float due to an insertion in another column

I have a DataFrame with two columns: a column of int and a column of str.

  • I understand that if I insert NaN into the int column, Pandas will convert all the int into float because there is no NaN value for an int.
  • However, when I insert None into the str column, Pandas converts all my int to float as well. This doesn’t make sense to me – why does the value I put in column 2 affect column 1?

Here’s a simple working example):

JavaScript

The output is:

JavaScript

Is there any way to make the output the following:

JavaScript

without recasting the first column to int.

  • I prefer using int instead of float because the actual data in that column are integers. If there’s not workaround, I’ll just use float though.

  • I prefer not having to recast because in my actual code, I don’t
    store the actual dtype.

  • I also need the data inserted row-by-row.

Advertisement

Answer

If you set dtype=object, your series will be able to contain arbitrary data types:

JavaScript
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement