简体   繁体   English

Pandas:用列最大值替换列中的所有值

[英]Pandas: Replace all values in column with column maximum

I have the following dataframe我有以下数据框

            NDQ        CFRI        NFFV        [more columns....]
2002-01-24 92.11310000 57.78140000 90.95720000
2002-01-25 57.97080000 91.05430000 58.19820000

I want to set all values in a column equal to the maximum value of the respective column.我想将列中的所有值设置为等于相应列的最大值。

Desired output:期望的输出:

            NDQ        CFRI        NFFV        [more columns....]
2002-01-24 92.11310000 91.05430000 90.95720000
2002-01-25 92.11310000 91.05430000 90.95720000

I have attempted to map it out to the result of df.max() , but struggled with the implementation and also feel like there would be an easier solution available.我试图将它映射到df.max()的结果,但在实现上df.max() ,并且觉得会有更简单的解决方案可用。

Any help would be greatly appreciated.任何帮助将不胜感激。

You can use df.clip and set the lower param to df.max :您可以使用df.clip并将lower参数设置为df.max

From Docs:从文档:

lower : float or array_like, default None Minimum threshold value. lower : float 或 array_like,默认 None 最小阈值。 All values below this threshold will be set to it.低于此阈值的所有值都将设置为它。

df.clip(df.max(),axis=1)

                NDQ     CFRI     NFFV
2002-01-24  92.1131  91.0543  90.9572
2002-01-25  92.1131  91.0543  90.9572

To set all values in a column equal to the maximum value of the respective column I would suggest to follow below code.要将列中的所有值设置为等于相应列的最大值,我建议遵循以下代码。

df['NDQ']=df.NDQ.max()

or或者

df['NDQ_Max']=df.NDQ.max()

df.clip is used to Assigns values outside boundary to boundary values. df.clip用于将边界外的值分配给边界值。 But as you want to set all the values in a particular values you may not need to use that.但是当您想在特定值中设置所有值时,您可能不需要使用它。 Please see the link请看链接

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

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

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