Pandas 在两个数据帧之间根据if条件生成新数据帧

Pandas 在两个数据帧之间根据if条件生成新数据帧,pandas,dataframe,Pandas,Dataframe,如果我有一个每月“点”数据框,其中的值来自cumsum(): 我有一个“购买”数据框,它基本上是指该月是否有购买: ID month1 month2 month3 month4 000 NO NO YES NO 111 NO YES NO YES 如何创建其值满足以下条件的新数据帧: IF points > 40 AND buy == "YES" THEN returns MAX(40,

如果我有一个每月“点”数据框,其中的值来自cumsum():

我有一个“购买”数据框,它基本上是指该月是否有购买:

ID   month1  month2  month3  month4
000  NO      NO      YES     NO
111  NO      YES     NO      YES     
如何创建其值满足以下条件的新数据帧:

IF points > 40 AND buy == "YES" 
THEN returns MAX(40, 0.8*points)
ELSE returns 0
生成的数据帧应为:

ID   month1  month2  month3  month4
000  0       0       40      0
111  0       48      0       41.6

ID 111的month4值为41.6,因为它在前几个月还剩下12个点,在本月再加上40个点,所以它是52*0.8=41.6

df = df1.merge(df2, on='ID')
然后使用np.where:

df['month1_x'] = np.where((df['month1_x'] > 40) & (df['month1_y'] == 'YES'), MAX(40, 0.8*df['month1_x']), 0)

尝试
np.where
并分配所有列:

准备:

df1 =pd.read_csv(io.StringIO('''ID   month1  month2  month3  month4
000  0       10      45      55
111  40      60      100     100'''),sep='\s+')
df1

df2 = pd.read_csv(io.StringIO('''ID   month1  month2  month3  month4
000  NO      NO      YES     NO
111  NO      YES     NO      YES     '''),sep='\s+')
df2
df2 = df2.set_index('ID')
df = df1.set_index('ID') 
condition = (df *0.8 > 40) & (df2== 'YES')
df[df.columns] = np.where(condition, df.values, 0)

df[df.columns] = np.where(df*0.8>0,df,np.nan)
ffill = df.ffill(axis=1) - df.ffill(axis=1).shift(1,axis=1)*0.8

df[df.columns] = np.where(((df.isna())|(ffill.isna())),df,ffill)
df = (df.fillna(0)*0.8).reset_index()
    ID  month1  month2  month3  month4
0   0   0.0     0.0     0.0     0.0
1   111 0.0     48.0    0.0     41.6
代码:

df1 =pd.read_csv(io.StringIO('''ID   month1  month2  month3  month4
000  0       10      45      55
111  40      60      100     100'''),sep='\s+')
df1

df2 = pd.read_csv(io.StringIO('''ID   month1  month2  month3  month4
000  NO      NO      YES     NO
111  NO      YES     NO      YES     '''),sep='\s+')
df2
df2 = df2.set_index('ID')
df = df1.set_index('ID') 
condition = (df *0.8 > 40) & (df2== 'YES')
df[df.columns] = np.where(condition, df.values, 0)

df[df.columns] = np.where(df*0.8>0,df,np.nan)
ffill = df.ffill(axis=1) - df.ffill(axis=1).shift(1,axis=1)*0.8

df[df.columns] = np.where(((df.isna())|(ffill.isna())),df,ffill)
df = (df.fillna(0)*0.8).reset_index()
    ID  month1  month2  month3  month4
0   0   0.0     0.0     0.0     0.0
1   111 0.0     48.0    0.0     41.6
输出:

df1 =pd.read_csv(io.StringIO('''ID   month1  month2  month3  month4
000  0       10      45      55
111  40      60      100     100'''),sep='\s+')
df1

df2 = pd.read_csv(io.StringIO('''ID   month1  month2  month3  month4
000  NO      NO      YES     NO
111  NO      YES     NO      YES     '''),sep='\s+')
df2
df2 = df2.set_index('ID')
df = df1.set_index('ID') 
condition = (df *0.8 > 40) & (df2== 'YES')
df[df.columns] = np.where(condition, df.values, 0)

df[df.columns] = np.where(df*0.8>0,df,np.nan)
ffill = df.ffill(axis=1) - df.ffill(axis=1).shift(1,axis=1)*0.8

df[df.columns] = np.where(((df.isna())|(ffill.isna())),df,ffill)
df = (df.fillna(0)*0.8).reset_index()
    ID  month1  month2  month3  month4
0   0   0.0     0.0     0.0     0.0
1   111 0.0     48.0    0.0     41.6

很抱歉问了更多问题,如何使真实结果为max(df.values*0.8,40)?还有一种方法可以更新后续的值(从cumsum()中减去已经“使用”的值),这样就和我的示例中一样了吗?谢谢,我很快就会看到的。。无论如何,您可以使用np.max和df.values*0.8…只需将
df.values
更改为
np.max(df.values*0.8,40)
np.where
中,并告诉我是否进行顺利。。只是不在电脑前..是的,np.maximum工作得很好!是否有相应地更新后续列的值的方法?我想让它变成这样:ID111的month4值是41.6,因为它在前几个月还剩下12点,在本月再加上40点,所以它是52*0.8=41.6