I ran into a problem like "value of a DataFrame is ambiguous". I tried to use logical indexing and special symbols like '&' but it doesn't help.
I have a table
| minuts | seconds | total_sec | cost | total_cost |
| ------------ | ------------ | ------------ | ------------ | ------------ |
| 1 | 49 | 109 | 1.5 | |
| 0 | 57 | 57 | 0.0 | |
| 0 | 34 | 34 | 0.0 | |
| 2 | 0 | 120 | 2.0 | |
| 0 | 55 | 55 | 0.0 | |
| 6 | 47 | 407 | 4.0 | |
I need to fill in the last column (cost) based on the following logic:
- if minuts >= 1 and seconds >= 1 then total_cost = cost + 0.5
- if minuts < 1 and seconds >= 1 then total_cost = cost + 1.5
- if minuts < 1 and seconds < 1 then total_cost = cost
I tried this code, but it doesn't work:
def check (minuts, seconds):
if df.loc[(df['minuts'] >= 1) & (df['seconds'] >= 1)]:
return df['total_cost'] + 0.5
if df.loc[(df['minuts'] < 1) & (df['seconds'] >= 1)]:
return df['total_cost'] + 1.5
else: return df['cost']
3条答案
按热度按时间qvsjd97n1#
以下是使用
np.where()
的方法:这比服装函数import numpy as np
更高效roejwanj2#
按行应用所需条件以计算
total_cost
值:6uxekuva3#
当你执行
df.loc[(df['minuts'] >= 1) & (df['seconds'] >= 1)]
时,你会得到满足第一个条件的子集,但是当你把if
加到它上面时:if df.loc[(df['minuts'] >= 1) & (df['seconds'] >= 1)]:
现在您尝试计算
if <dataframe>:
,并且正如错误所述,DataFrame的真值是不明确的。一种不需要额外导入的解决方案是获取与每个条件对应的索引,然后使用
iloc
赋值:注意,我使用了
query
,但它应该可以与q1 = df.loc[(df['minuts'] >= 1) & (df['seconds'] >= 1)].index
一起使用。