简体   繁体   English

如何根据另一列的值更改 Pandas DataFrame 中的值

[英]How to change values in a Pandas DataFrame based on values of another columns

I have the following DataFrame with some numbers in them where the sum of the values in Col1, Col2, and Col3 is equal to the value in column Main.我有以下 DataFrame,其中包含一些数字,其中 Col1、Col2 和 Col3 中的值之和等于 Main 列中的值。

How can I replace the values in the Cat columns if they are equal to the corresponding value in the Main column?如果Cat列中的值等于Main列中的相应值,我该如何替换它们?

For example, the following DataFrame:例如,以下 DataFrame:

      Main        Col1        Col2        Col3
0     100         50          50          0
1     200         0           200         0
2     30          20          5           5
3     500         0           0           500

would be changed to this:将更改为:

      Main        Col1        Col2        Col3
0     100         50          50          0
1     200         0           EQUAL       0
2     30          20          5           5
3     500         0           0           EQUAL

You can use filter to apply only on the "Col" columns (you could also use slicing with a list, see alternative), then mask to change the matching values, finally update to update the DataFrame in place:您可以使用filter仅在“Col”列上应用(您也可以将切片与列表一起使用,请参阅替代方法),然后使用mask更改匹配值,最后update以更新 DataFrame:

df.update(df.filter(like='Col').mask(df.eq(df['Main'], axis=0), 'EQUAL'))

Alternative:选择:

cols = ['Col1', 'Col2', 'Col3']
df.update(df[cols].mask(df.eq(df['Main'], axis=0), 'EQUAL'))

Output:输出:

   Main  Col1   Col2   Col3
0   100    50     50      0
1   200     0  EQUAL      0
2    30    20      5      5
3   500     0      0  EQUAL

There are several different ways of doing this, I suggest using the np.where() function.有几种不同的方法可以做到这一点,我建议使用np.where()函数。

import numpy as np

df['Col1'] = np.where(df['Col1'] == df['Main'], 'EQUAL', df['Col1']
df['Col2'] = np.where(df['Col2'] == df['Main'], 'EQUAL', df['Col2']
df['Col3'] = np.where(df['Col3'] == df['Main'], 'EQUAL', df['Col3']

Read more about np.where() here . 在此处阅读有关np.where()的更多信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何根据熊猫中的另一个更改数据框中的值? - How change values in dataframe based on another in pandas? 如何根据 Pandas 中的另一个 DataFrame 更改 DataFrame 特定列中的值 - How to change values in certain column of DataFrame based on another DataFrame in Pandas 根据来自另一个 DataFrame 的值更新 pandas 列中的值 - Update values in pandas columns based on values from another DataFrame 子集根据另一个数据帧的值在多个列上进行pandas数据帧 - Subset pandas dataframe on multiple columns based on values from another dataframe 将值添加到基于另一个 dataframe 的 pandas dataframe 列 - adding values to pandas dataframe columns based on another dataframe 根据来自另一个数据框的数据将值分配给Pandas数据框中的列 - Assign values to columns in Pandas Dataframe based on data from another dataframe 根据另一个数据帧将列添加到 Pandas 数据帧并将值设置为零 - Add columns to Pandas dataframe based on another dataframe and set values to zero 根据 dataframe 中的其他列更改 pandas dataframe 列值 - Change pandas dataframe column values based on other columns in dataframe 熊猫-根据其他数据框列中的值删除列 - Pandas - Remove Columns based on values in another dataframe columns 如何根据另一个 dataframe 中的查找值替换 pandas dataframe 值? - How to replace pandas dataframe values based on lookup values in another dataframe?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM