Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do you divide pandas columns by a scalar?

Tags:

pandas

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?

like image 959
Nils Wagenaar Avatar asked Oct 31 '16 02:10

Nils Wagenaar


2 Answers

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

like image 176
SamithaP Avatar answered Oct 07 '22 21:10

SamithaP


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.

like image 18
Back2Basics Avatar answered Oct 07 '22 21:10

Back2Basics