Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
205 views
in Technique[技术] by (71.8m points)

python - Stop Pandas from converting int to float

I have a DataFrame. Two relevant columns are the following: one is a column of int and another is 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 (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

The output is

   int   str
0    0  zero

   int   str
0  0.0  zero
1  1.0   NaN

Is there any way to make the output the following:

   int   str
0    0  zero

   int   str
0    0  zero
1    1   NaN

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.

question from:https://stackoverflow.com/questions/40251948/stop-pandas-from-converting-int-to-float

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you set dtype=object, your series will be able to 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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...