简体   繁体   English

根据多个条件乘以pandas dataframe列

[英]Multiplying pandas dataframe column according to multiple conditions

I would like to multiply a column (or create a new one with the multiplied values) based on two conditions.我想根据两个条件将一列相乘(或用相乘的值创建一个新列)。 So I tried:所以我尝试了:

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

Here is the dataframe这是 dataframe

    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

But it returned:但它返回了:

---------------------------------------------------------------------------
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().

It's a not clear what exact values you want.目前尚不清楚您想要什么确切的值。 If you want to have c1 and c2 as Series with the mean for each groups, you can what is below.如果您想将 c1 和 c2 作为系列,每个组的平均值,您可以使用以下内容。 If you only want one output (float) you can use groupby.如果您只想要一个 output (float),您可以使用 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

Groupby:通过...分组:

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

The or and and python statements require truth-values. or and and and python 语句需要真值。 For pandas these are considered ambiguous so you should use "bitwise" |对于 pandas 这些被认为是模棱两可的,所以你应该使用“按位”| (or) or & (and) operations: Ref (or) 或 & (and) 操作: 参考

Replace c1 and c2 with:将 c1 和 c2 替换为:

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

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

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