buhtz
buhtz

Reputation: 12152

Why does pandas make a float64 out of an integer?

I set an integer as a cell value but Pandas does make a float64 out of it. The question is why and how I can prevent that?

>>> df = pandas.DataFrame()
>>> df.loc['X', 'Y'] = 1
>>> df
     Y
X  1.0

Upvotes: 0

Views: 34

Answers (1)

P.Jo
P.Jo

Reputation: 702

I think you shouldn't start with an empty DataFrame and then use df.loc[] to create columns.

E.g. start with a df with dtypes set

df = pandas.DataFrame({"x":[1,2,3]}, dtype=np.int64)

or if you need to adjust the dtype of a column, then call the astype() method, e.g.

df["x"] = df["x"].astype("float")

Upvotes: 2

Related Questions