【问题标题】:Remove Values of Data Frame based on Other Column Values根据其他列值删除数据框的值
【发布时间】:2021-09-25 15:49:53
【问题描述】:

我想根据另一列中的值从数据框的特定列中删除单元格值。

对于“匹配”列中包含“TRUE”的每一行,应删除“品牌总差异”列中的值(不是零,只是空)。

variable Difference Magnitude Total Difference by Brand Total Difference by Location match
Total New Funding Sources 1406904 5031189 4373182 FALSE
Total New Funding Sources 75821 5031189 4373182 TRUE
Total New Funding Sources 33692 5031189 4373182 TRUE
Total New Funding Sources 2627094 5031189 4373182 TRUE
Total New Funding Sources 400000 500000 4373182 FALSE
Total New Funding Sources 500000 500000 4373182 TRUE
Total New Funding Sources 1406904 5131189 4373182 FALSE
Total New Funding Sources 75821 5131189 4373182 TRUE
Total New Funding Sources 33692 5131189 4373182 TRUE

数据框应如下所示:

variable Difference Magnitude Total Difference by Brand Total Difference by Location match
Total New Funding Sources 1406904 5031189 4373182 FALSE
Total New Funding Sources 75821 4373182 TRUE
Total New Funding Sources 33692 4373182 TRUE
Total New Funding Sources 2627094 4373182 TRUE
Total New Funding Sources 400000 500000 4373182 FALSE
Total New Funding Sources 500000 4373182 TRUE
Total New Funding Sources 1406904 5131189 4373182 FALSE
Total New Funding Sources 75821 4373182 TRUE
Total New Funding Sources 33692 4373182 TRUE

谢谢

【问题讨论】:

    标签: python-3.x pandas dataframe duplicates conditional-statements


    【解决方案1】:

    你可以试试这个:

    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
    

    【讨论】:

    • 不幸的是,这不起作用。它没有为我改变数据集中的值
    • 很可能是因为您的数据框 dtypes,它必须与我的示例中的不同。你能发布df.head(9).to_dict()的输出吗?
    • 'variable': {0: '新资金来源总数', 1: '新资金来源总数', 2: '新资金来源总数', 3: '新资金来源总数'}, “差异幅度”:{0:1406904.0、1:75821.0、2:33692.0、3:2627094.0}、“品牌总差异”:{0:5031189.0、1:5031189.0、2:5031189.0、3:5031189}总计。位置差异': {0: 4373182.0, 1: 4373182.0, 2: 4373182.0, 3: 4373182.0}, 'match': {0: False, 1: True, 2: True, 3: True}} 我做了前四个行,因为 9 行太多,无法放入评论字段
    • 还有 dtypes:variable 对象,Difference Magnitude float64,Total Difference by Brand 对象,Total位置差异 float64, match bool.
    • 好的,所以你有 bool 值 (True) 而不是字符串值 (TRUE),因为你的问题可能让人想一想。请参阅我更新的答案,它适用于您提供的数据。
    猜你喜欢
    • 1970-01-01
    • 2018-03-11
    • 1970-01-01
    • 2021-10-03
    • 1970-01-01
    • 1970-01-01
    • 2021-08-09
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多