根据多个条件相乘数据帧列

2024-05-23 19:58:01 发布

您现在位置:Python中文网/ 问答频道 /正文

我想根据两个条件乘以一个列(或者创建一个具有乘以值的新列)。所以我试着:

c1 = df['Mean']=='SEPA' and df['Engagement'] == 'M'
c2 = df['Mean']!='SEPA' and df['Engagement'] == 'M'
df.loc[c1, ['Amount Eq Euro']] *= 62
df.loc[c2, ['Amount Eq Euro']] *= 18

这是数据帧

    Mean    Engagement  Amount Eq Euro
2   CB (PAYPAL) S   50.0
3   CB  S   50.0
4   CB  S   50.0
5   CB (PAYPAL) M   20.0
6   CB  S   75.0
... ... ... ...
6238    CB  S   30.0
6239    CB  S   80.0
6240    SEPA    M   10.0
6241    CB  S   100.0
6242    NaN M   10.0

但它返回:

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-13-bbd424e0088f> in <module>()
      3 #                                            df['Amount Eq Euro'] * 18)
      4 
----> 5 c1 = df['Mean']=='SEPA' and df['Engagement'] == 'M'
      6 c2 = df['Mean']!='SEPA' and df['Engagement'] == 'M'
      7 df.loc[c1, ['Amount Eq Euro']] *= 62

/usr/local/lib/python3.7/dist-packages/pandas/core/generic.py in __nonzero__(self)
   1328     def __nonzero__(self):
   1329         raise ValueError(
-> 1330             f"The truth value of a {type(self).__name__} is ambiguous. "
   1331             "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
   1332         )

ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().

Tags: andselfdfmeanamountpaypalloceq
2条回答

The or and and python statements require truth-values. For pandas these are considered ambiguous so you should use "bitwise" | (or) or & (and) operations: Ref

将c1和c2替换为:

c1 = (df['Mean']=='SEPA') & (df['Engagement'] == 'M')
c2 = (df['Mean']!='SEPA') & (df['Engagement'] == 'M')

目前还不清楚您想要的确切值。若你们想把c1和c2作为一系列,每组的平均数,你们可以选择下面的。如果只需要一个输出(浮点),可以使用groupby

c1 = df.loc[(df['Mean'] == 'SEPA') & (df['Engagement'] == 'M', 'Amount Eq Euro').mean()
c2 = df.loc[(df['Mean'] != 'SEPA') & (df['Engagement'] == 'M', 'Amount Eq Euro').mean()
c = c1 + c2
df.loc[c, 'Amount Eq Euro'] *= 62
df.loc[c, 'Amount Eq Euro'] *= 18

分组人:

c = df.loc[df['Engagement'] == 'M'].groupby('SEPA')['Amount Eq Euro'].mean()

相关问题 更多 >