Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 401
M. Vlad Avatar asked Sep 06 '25 03:09

M. Vlad


2 Answers

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
like image 137
anky Avatar answered Sep 07 '25 20:09

anky


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

like image 37
Md Ziaul Haque Avatar answered Sep 07 '25 19:09

Md Ziaul Haque