[英]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:我的 dataframe 如下所示,我需要将每个单元格除以其列的最大值:
Col1 Col2
5 10
20 20
25 30
100 40
Output should look like: Output 应如下所示:
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:现在,将 DataFrame 的每个元素除以相应列的最大值:
df = df.div(max_value)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.