0

Last time I tried to put a nan into a Pandas dataframe, it forced me to change the column from type int to float.

In SQL there is not an issue with with having a 'NULL' in a column of any type as far as I know. The dataframes I am working with often go in and out of SQL.

Now I have a dataframe with columns including int, object and float and need to create some code which programatically adds a few single rows where 6 out of 7 columns should contain nothing and only 1 out of 7 is assigned a value.

Is there some other standard 'NULL' thing in Pandas you can put in the columns that are not of type float?

This time I definitely can't go and change the type of a column just to put an nan in it.

cardamom
  • 6,873
  • 11
  • 48
  • 102

1 Answers1

0

If you add a row and do not mention the other columns explicitly as per this answer, it just creates NaN's even in the columns which are not of type float64.

res = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))
res = res.append([{'qty1':10.0}], ignore_index=True)
print(res.head())

   lib  qty1  qty2
0  NaN  10.0   NaN
cardamom
  • 6,873
  • 11
  • 48
  • 102