简体   繁体   English

Pandas:根据条件将数据从df提取到新的df

[英]Pandas: extract data from df to a new df based on condition

I have the following df1:我有以下df1:

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

And then another df2:然后另一个df2:

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

So these two dataframes have the same index and columns.所以这两个数据框具有相同的索引和列。 How can I select elements of df1 that only have "c" in df2?我怎样才能 select 的 df1 元素在 df2 中只有“c”?

The result should be values from df1 when the condition is met, and 0 otherwise:满足条件时,结果应该是来自 df1 的值,否则为 0:

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

If first column is index use DataFrame.where :如果第一列是索引使用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

If first column is not index, one possible idea is select all columns without first and assign back:如果第一列不是索引,一个可能的想法是 select 所有没有第一列的列并分配回:

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

Or:或者:

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

With @jezrael's help, another solution using np.where :在@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()

Output: Output:

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

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM