【问题标题】:Pandas: extract data from df to a new df based on conditionPandas:根据条件将数据从df提取到新的df
【发布时间】:2020-06-02 20:25:30
【问题描述】:

我有以下 df1:

Person  Day1  Day2  Day3
1       2     1     1
2       2     0     7
3       4     1     2

然后是另一个 df2:

Person  Day1  Day2  Day3
1       a     b     b
2       a     c     a
3       c     b     c

所以这两个数据框具有相同的索引和列。如何选择 df2 中只有“c”的 df1 元素?

当条件满足时,结果应该是来自 df1 的值,否则为 0:

Person  Day1  Day2  Day3
1       0     0     0
2       0     0     0
3       4     0     2

【问题讨论】:

    标签: python pandas dataframe


    【解决方案1】:

    如果第一列是索引使用DataFrame.where:

    df = df1.where(df2.eq('c'), 0)
    print (df)
            Day1  Day2  Day3
    Person                  
    1          0     0     0
    2          0     0     0
    3          4     0     2
    

    如果第一列不是索引,一个可能的想法是选择没有第一列的所有列并分配回:

    df1.iloc[:, 1:]= df1.iloc[:, 1:].where(df2.iloc[:, 1:].eq('c'), 0)
    print (df1)
       Person  Day1  Day2  Day3
    0       1     0     0     0
    1       2     0     0     0
    2       3     4     0     2
    

    或者:

    df = df1.set_index('Person').where(df2.set_index('Person').eq('c'), 0).reset_index()
    print (df)
       Person  Day1  Day2  Day3
    0       1     0     0     0
    1       2     0     0     0
    2       3     4     0     2
    

    【讨论】:

    • 很好的答案。 np.where也可以做同样的事情吗?
    • @MayankPorwal - 是的,然后需要DataFrame 构造函数,例如df = pd.DataFrame(np.where(df2.eq('c'), df1, 0), index=df1.index, columns=df1.index)
    • 感谢 +1 的帮助。
    【解决方案2】:

    在@jezrael 的帮助下,另一个使用np.where的解决方案:

    import numpy as np
    df = pd.DataFrame(np.where(df2.eq('c'), df1, 0), index=df1.index, columns=df1.columns).reset_index()
    

    输出:

       Person  Day1  Day2  Day3
    0       1     0     0     0
    1       2     0     0     0
    2       3     4     0     2
    

    【讨论】:

      猜你喜欢
      • 2023-01-30
      • 1970-01-01
      • 2020-06-09
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2020-09-16
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多