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