简体   繁体   中英

Divide each cell in dataframe by max of of that column in pandas

My dataframe looks like below and i need to divide each cell by max of its column:

Col1  Col2
5      10
20     20
25     30
100    40

Output should look like:

Col1   Col2
1/20    1/4
1/5     1/2
1/4     3/4
1       1

It is as simple as:

df = pd.DataFrame.from_dict({'Col1': [5, 20, 25, 100], 'Col2': [10, 20, 30, 40]})
df / df.max()

Try the following.

df.apply(lambda x: x / x.max())

This divides each column by the maximum value found in each column

First, compute the max value per column:

max_value = df.max()

Now, divide each element of the DataFrame by the max value of the respective column:

df = df.div(max_value)

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