Skip to content
Advertisement

create an int column after dividing a column by a number in pandas

Assume that I have a panda data frame with a column that holds seconds.

I want to create a new column that holds minutes. so I divide the sec column by 60. The problem that I have is that min column is not an integer anymore. How can I make it an integer?

I have this code:

alldata['min']=alldata['sec']/60

I tried this, but it did not work:

alldata['min']=int(alldata['sec']/60)

I am getting this error:

TypeError: cannot convert the series to <class 'int'>

I tried this:

alldata["min"] = pd.to_numeric(alldata["min"], downcast='integer')

but I still have float values in min column.

How can have integer values for min? (Just drop the value to its floor value)

Advertisement

Answer

You should just use whole integer division:

alldata['min']=alldata['sec']//60
User contributions licensed under: CC BY-SA
9 People found this is helpful
Advertisement