Py学习  »  Python

python中货币的if/then语句

data.mama • 3 年前 • 1323 次点击  

正在尝试以一种新的方式转换货币 df 使用python。 我有两个专栏:价格和货币。 我试着用 if/elif/else 但是我做错了什么。

df样本: |价格|货币|船型|建造年份| |-----|--------|---------|-----------| |3490欧元控制台2020欧元| |2299欧元捕鱼2019欧元| |3500 |瑞士法郎|捕鱼| 1987| |4600 | | runabout | 2020|

如有任何建议,我们将不胜感激。非常感谢。

我试过的代码。。。

if drop_boats['Currency'] == 'EUR':
    drop_boats['Price'] = drop_boats['Price'] * 1.10 
elif drop_boats['Currency'] == 'CHF':
    drop_boats['Price'] = drop_boats['Price'] * 1.08 
elif drop_boats['Currency'] == 'DKK':
    drop_boats['Price'] = drop_boats['Price'] * 0.15 
elif drop_boats['Currency'] == '£':
    drop_boats['Price'] = drop_boats['Price'] * 1.32
else:
     drop_boats['Price' ]= drop_boats['Price']

我得到了这个错误: ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all() .

Python社区是高质量的Python/Django开发社区
本文地址:http://www.python88.com/topic/130054
 
1323 次点击  
文章 [ 3 ]  |  最新文章 3 年前
spitoglou
Reply   •   1 楼
spitoglou    3 年前

实现你想要的一种方法是:

for index, row in drop_boats.iterrows():
  if row['Currency'] == 'EUR':
    drop_boats.loc[index, 'Price'] = row['Price'] * 1.10

您可以相应地添加其他条件。。。

timgeb
Reply   •   2 楼
timgeb    3 年前

你可能想要 loc 使用布尔索引。如果没有显示样本输入和输出数据,请。

drop_boats.loc[drop_boats['Currency'].eq('EUR'), 'Price'] *= 1.10
Vaibhav
Reply   •   3 楼
Vaibhav    3 年前

你可以使用iter在熊猫排上进行测试 iter.rows() 并进行计算。请参阅以下代码:

for index, row in df.iterrows():
    if row['Currency'] == 'EUR':
        row['Price'] = row['Price'] * 1.10 
    elif row['Currency'] == 'CHF':
        row['Price'] = row['Price'] * 1.08 
    elif row['Currency'] == 'DKK':
        row['Price'] = row['Price'] * 0.15 
    elif row['Currency'] == '£':
        row['Price'] = row['Price'] * 1.32
    else:
        row['Price' ]= row['Price']