简体   繁体   中英

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.

Any help would be greatly appreciated.

You can use df.clip and set the lower param to df.max :

From Docs:

lower : float or array_like, default None Minimum threshold value. 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. 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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