Chique_Code
Chique_Code

Reputation: 1530

divide the values from pandas df column by number

I need to divide every value of a column by 25. Below is my code:

df['Amount'] = df['Amount'].div(25)

I've gotten an error: TypeError: unsupported operand type(s) for /: 'str' and 'int' Then I tried to convert Object datatype to int with the following code:

df["Amount"] = df["Amount"].astype(str).astype(int)

The error message: ValueError: invalid literal for int() with base 10: '0.0'

Upvotes: 2

Views: 5166

Answers (2)

BrunoSE
BrunoSE

Reputation: 94

You could try pd.to_numeric function, in case you want to specify it's a float column you could try the downcast parameter, although it will most likely be OK if you don't add it.

df["Amount"] = pd.to_numeric(df["Amount"], downcast='float')

Upvotes: 1

Soumendra Mishra
Soumendra Mishra

Reputation: 3653

You can try this:

df["Amount"] = df["Amount"]/25
print(df)

Upvotes: 2

Related Questions