简体   繁体   English

将 dataframe 中的每个单元格除以 pandas 中该列的最大值

[英]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.

相关问题 Python:在每列中将单元格中的值除以max - Python: Divide values in cell by max in each column 如何按系列划分pandas Dataframe的每一列? - How to divide each column of pandas Dataframe by a Series? 将 dataframe 列除以特定单元格 - Divide dataframe column by a specific cell pandas select 每列的最小值和最大值并创建一个新的 dataframe - pandas select the minimum and max of each column and create a new dataframe Python pandas dataframe:为另一列的每个唯一值查找最大值 - Python pandas dataframe: find max for each unique values of an another column 将Pandas数据框中的列除以列总和 - Divide Column in Pandas Dataframe by Sum of Column Pandas DataFrame 如何将列的每个单元格与另一个列中的每个单元格进行比较 dataframe 并删除匹配的列 - Pandas DataFrame how to compare each cell of a colum with each cell of another column in another dataframe and drop matching ones Pandas:在 dataframe 的列中过滤每个唯一单元格值的日期字段 - Pandas: Filter datefield for each unique cell values in a column in dataframe 将函数应用于DataFrame中的每个单元格,这取决于pandas中的列名称 - Apply function to each cell in DataFrame that depends on the column name in pandas 在 Pandas DataFrame 的单元格内为列表中的每个项目添加一个新列 - Add a new column for each item in a list inside a cell in a Pandas DataFrame
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM