我想根据两个条件乘以一个列(或者创建一个具有乘以值的新列)。所以我试着:
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().
将c1和c2替换为:
目前还不清楚您想要的确切值。若你们想把c1和c2作为一系列,每组的平均数,你们可以选择下面的。如果只需要一个输出(浮点),可以使用groupby
分组人:
相关问题 更多 >
编程相关推荐