Stop Pandas from converting int to float

I have a DataFrame . Two corresponding columns: the int column and the other the str column.

I understand that if I insert NaN into an int column, Pandas converts all int to float , because there is no NaN value for int .

However, when I insert None into the str column, Pandas also converts all my int to float . This doesn't make sense to me - why does the value that I put in column 2 affect column 1?

Here is a simple working example (Python 2):

 import pandas as pd df = pd.DataFrame() df["int"] = pd.Series([], dtype=int) df["str"] = pd.Series([], dtype=str) df.loc[0] = [0, "zero"] print df print df.loc[1] = [1, None] print df 

Output signal

  int str 0 0 zero int str 0 0.0 zero 1 1.0 NaN 

Is there any way to conclude as follows:

  int str 0 0 zero int str 0 0 zero 1 1 NaN 

without translating the first column to int .

  • I prefer to use int instead of float , because the actual data in this column is an integer. If there is no workaround, I just use float though.

  • I prefer not to redo it because in my actual code I don't save the actual dtype .

  • I also need data inserted row by row.

+12
python pandas
source share
3 answers

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

 df["int"] = pd.Series([], dtype=object) df["str"] = pd.Series([], dtype=str) df.loc[0] = [0, "zero"] print(df) print() df.loc[1] = [1, None] print(df) int str 0 0 zero 1 NaN NaN int str 0 0 zero 1 1 None 
+18
source share

this works just as well:

 df["int"] = df["int"].astype(int) 

with https://stackoverflow.com/a/312960/

EDIT: This does not work so well when there are spaces in the column :(

0
source share

If you use DataFrame.append to add data, dtypes are saved, and you do not need to redo or rely on object :

 In [157]: df Out[157]: int str 0 0 zero In [159]: df.append(pd.DataFrame([[1, None]], columns=['int', 'str']), ignore_index=True) Out[159]: int str 0 0 zero 1 1 None 
0
source share

All Articles