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.
python pandas
user2570465
source share