Home > Back-end >  I am expecting output as int, but getting float
I am expecting output as int, but getting float

Time:07-25

I try to make a new Column named 'Years' from a data frame in python

my code is:

df['Years'] = df['Days']//365.

I want to get the output as int so I use '//' but why the output I get is in float = 13.0

CodePudding user response:

Try this:

df['Years'] = (df['Days']//365).astype('int32')

CodePudding user response:

Does the actual data type matter to you? // does flooring division, but the data type for your Series remains float.

If you really want integers, cast the series.

import pandas as pd

df = pd.DataFrame({"days": [5303.0, 6604.1, 64202.2, 93.3]})
df["years"] = (df["days"] // 365).astype(int)
print(df.years)
0     14
1     18
2    175
3      0
Name: years, dtype: int64

CodePudding user response:

It depends on the value that is present in the numerator. In this case the value in Days column is probably float.

  • Related