pandas 你如何用标量划分熊猫列?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/40335695/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 02:19:11  来源:igfitidea点击:

How do you divide pandas columns by a scalar?

pandas

提问by Nils Wagenaar

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:

我在Pandas数据框中有一个来自 excel 的文件。我有很多列和行,我想将所有列和行值(NaN 除外)除以标量(2.45)。这就是我所拥有的:

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?

它没有给我一个错误,但这些值没有在 ET 中划分。有人有解决方案吗?

回答by Back2Basics

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

如果整个 DataFrame 是数字,您可以一次将所有值(甚至是 NaN)除以 2.45

df= df/2.45
print(df)

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

请注意,我必须用 替换 DataFramedf =以使其保持不变。