根据其他列值删除数据框的值
Remove Values of Data Frame based on Other Column Values
我想根据另一列中的值从数据框的特定列中删除单元格值。
对于第 'match' 列中包含“TRUE”的每一行,应删除第 'Total Difference by Brand' 列中的值(不是零,只是空值)。
变量
差异幅度
品牌总差异
按位置的总差异
匹配
新资金来源总数
1406904
5031189
4373182
错误
新资金来源总数
75821
5031189
4373182
正确
新资金来源总数
33692
5031189
4373182
正确
新资金来源总数
2627094
5031189
4373182
正确
新资金来源总数
400000
500000
4373182
错误
新资金来源总数
500000
500000
4373182
正确
新资金来源总数
1406904
5131189
4373182
错误
新资金来源总数
75821
5131189
4373182
正确
新资金来源总数
33692
5131189
4373182
正确
数据框应如下所示:
变量
差异幅度
品牌总差异
按位置的总差异
匹配
新资金来源总数
1406904
5031189
4373182
错误
新资金来源总数
75821
4373182
正确
新资金来源总数
33692
4373182
正确
新资金来源总数
2627094
4373182
正确
新资金来源总数
400000
500000
4373182
错误
新资金来源总数
500000
4373182
正确
新资金来源总数
1406904
5131189
4373182
错误
新资金来源总数
75821
4373182
正确
新资金来源总数
33692
4373182
正确
谢谢
你可以试试这个:
import pandas as pd
df = pd.DataFrame(
{
"variable": {
0: "Total New Funding Sources",
1: "Total New Funding Sources",
2: "Total New Funding Sources",
3: "Total New Funding Sources",
},
"Difference Magnitude": {0: 1406904.0, 1: 75821.0, 2: 33692.0, 3: 2627094.0},
"Total Difference by Brand": {
0: 5031189.0,
1: 5031189.0,
2: 5031189.0,
3: 5031189.0,
},
"Total Difference by Location": {
0: 4373182.0,
1: 4373182.0,
2: 4373182.0,
3: 4373182.0,
},
"match": {0: False, 1: True, 2: True, 3: True},
}
)
df.loc[df["match"].eq(True), "Total Difference by Brand"] = ""
print(df[["variable", "Total Difference by Brand"]])
# Outputs
variable Total Difference by Brand
0 Total New Funding Sources 5031189.0
1 Total New Funding Sources
2 Total New Funding Sources
3 Total New Funding Sources
我想根据另一列中的值从数据框的特定列中删除单元格值。
对于第 'match' 列中包含“TRUE”的每一行,应删除第 'Total Difference by Brand' 列中的值(不是零,只是空值)。
变量 | 差异幅度 | 品牌总差异 | 按位置的总差异 | 匹配 |
---|---|---|---|---|
新资金来源总数 | 1406904 | 5031189 | 4373182 | 错误 |
新资金来源总数 | 75821 | 5031189 | 4373182 | 正确 |
新资金来源总数 | 33692 | 5031189 | 4373182 | 正确 |
新资金来源总数 | 2627094 | 5031189 | 4373182 | 正确 |
新资金来源总数 | 400000 | 500000 | 4373182 | 错误 |
新资金来源总数 | 500000 | 500000 | 4373182 | 正确 |
新资金来源总数 | 1406904 | 5131189 | 4373182 | 错误 |
新资金来源总数 | 75821 | 5131189 | 4373182 | 正确 |
新资金来源总数 | 33692 | 5131189 | 4373182 | 正确 |
数据框应如下所示:
变量 | 差异幅度 | 品牌总差异 | 按位置的总差异 | 匹配 |
---|---|---|---|---|
新资金来源总数 | 1406904 | 5031189 | 4373182 | 错误 |
新资金来源总数 | 75821 | 4373182 | 正确 | |
新资金来源总数 | 33692 | 4373182 | 正确 | |
新资金来源总数 | 2627094 | 4373182 | 正确 | |
新资金来源总数 | 400000 | 500000 | 4373182 | 错误 |
新资金来源总数 | 500000 | 4373182 | 正确 | |
新资金来源总数 | 1406904 | 5131189 | 4373182 | 错误 |
新资金来源总数 | 75821 | 4373182 | 正确 | |
新资金来源总数 | 33692 | 4373182 | 正确 |
谢谢
你可以试试这个:
import pandas as pd
df = pd.DataFrame(
{
"variable": {
0: "Total New Funding Sources",
1: "Total New Funding Sources",
2: "Total New Funding Sources",
3: "Total New Funding Sources",
},
"Difference Magnitude": {0: 1406904.0, 1: 75821.0, 2: 33692.0, 3: 2627094.0},
"Total Difference by Brand": {
0: 5031189.0,
1: 5031189.0,
2: 5031189.0,
3: 5031189.0,
},
"Total Difference by Location": {
0: 4373182.0,
1: 4373182.0,
2: 4373182.0,
3: 4373182.0,
},
"match": {0: False, 1: True, 2: True, 3: True},
}
)
df.loc[df["match"].eq(True), "Total Difference by Brand"] = ""
print(df[["variable", "Total Difference by Brand"]])
# Outputs
variable Total Difference by Brand
0 Total New Funding Sources 5031189.0
1 Total New Funding Sources
2 Total New Funding Sources
3 Total New Funding Sources