简体   繁体   English

将函数应用于 Pandas Dataframe 的单列

[英]Apply function to single column of pandas Dataframe

I am trying to apply a function to a single column of my dataframe (specifically, normalization).我正在尝试将一个函数应用于我的数据框的单列(特别是标准化)。

The dataframe looks like this:数据框如下所示:

     Euclidian        H         N       Volume
222   0.012288  0.00518  0.011143   85203000.0
99    1.296833 -0.80266  1.018583   17519400.0
98    1.618482 -0.60979  1.499213   16263900.0
211   2.237388  0.38073 -2.204757   38375400.0
175   2.313548  0.35656 -2.285907   66974200.0
102   3.319342  3.01295 -1.392897   33201000.0
7     3.424589 -0.31313  3.410243   97924700.0
64    3.720370 -0.03526  3.720203  116514000.0
125   3.995138  0.27396  3.985733   80526200.0
210   4.999969  0.46453  4.978343   70612100.0

The dataframe is named 'discrepancies', and my code is as such:数据框名为“差异”,我的代码如下:

max = discrepancies['Volume'].max()
discrepancies['Volume'].apply(lambda x: x/max)
return discrepancies

But the column values do not change.但列值不会改变。 I cannot find anywhere in the documentation to apply to single columns, they only talk about applying to all columns or all rows:我在文档中找不到任何适用于单列的地方,他们只谈到适用于所有列或所有行:

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.apply.html

Thank you谢谢

If it is just a single column, you don't need to use apply .如果它只是一列,则不需要使用apply Directly divide the column using its max will do.使用其最大值直接划分列即可。

discrepancies['Volume'] = discrepancies['Volume'] / discrepancies['Volume'].max()

Since single columns do not need apply also we need assign it back由于单列不需要apply我们也需要将其分配回来

max = discrepancies['Volume'].max()
discrepancies['some col']=discrepancies['Volume']/max

Also series you can use map您还可以使用地图系列

max = discrepancies['Volume'].max()
discrepancies['Volume'].map(lambda x: x/max)

the problem with your code is that pandas.apply returns the result as new data frame.您的代码的问题在于pandas.apply将结果作为新数据框返回。 (there is inplace attribute for lots of pandas functions but not apply ) (许多熊猫函数都有inplace属性,但不apply

to correct you code you should do:要更正您的代码,您应该这样做:

max = discrepancies['Volume'].max()
discrepancies['Volume'] = discrepancies['Volume'].apply(lambda x: x/max)
return discrepancies

or you can use @YOBEN_S answer.或者您可以使用@YOBEN_S 答案。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM