Nils Wagenaar
Nils Wagenaar

Reputation: 121

How do you divide pandas columns by a scalar?

I have a file from excel in a pandas dataframe. I have many columns and rows and I want to divide all column and row values (except NaN) by a scalar(2.45). This is what i have:

df = pd.read_excel('Ozflux.dailyLE.xlsx', sheetname='Sheet1', skiprows=0, index_col=2, na_values=[-9999])
ET = df.iloc[4: , 3: ]/2.45
print (ET)

It doesn't give me an error but the values are not divided in ET. Anyone has solution?

Upvotes: 12

Views: 40958

Answers (2)

SamithaP
SamithaP

Reputation: 100

first, it is better to make a copy of your original data frame. Then you can divide any column by scalar as follows. Assume there is a column named ET

new_dataframe=df[:]
new_dataframe['ET']=new_dataframe['ET]'/2.45

Upvotes: 3

Back2Basics
Back2Basics

Reputation: 7806

If the whole DataFrame is numerical you can divide all the values (even the NaN's) by 2.45 at once

df= df/2.45
print(df)

Notice I had to replace the DataFrame with df = to make it stick.

Upvotes: 19

Related Questions