Home > Back-end >  create an int column after dividing a column by a number in pandas
create an int column after dividing a column by a number in pandas

Time:02-12

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)

CodePudding user response:

You should just use whole integer division:

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

CodePudding user response:

You can use .astype() to convert dataframe collumns to diffenrent types. In your case use the following line:

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

DocPage

CodePudding user response:

Try

alldata['min'] = alldata['min'].astype('int64')

or .astype('int32') Based on your operating system

  • Related