嘗試df使用 python 轉換貨幣。我有兩列:價格和貨幣。我嘗試使用if/elif/else陳述句,但我做錯了什么。
Sample of df:
|price|currency|boat type| year built|
|-----|--------|---------|-----------|
|3490 |EUR |console | 2020 |
|2299 |EUR |fishing | 2019 |
|3500 |CHF |fishing | 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()。
uj5u.com熱心網友回復:
iter.rows()您可以使用并進行計算遍歷 pandas 行。參考以下代碼:
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']
uj5u.com熱心網友回復:
您可能需要loc布爾索引。如果不顯示示例輸入和輸出資料,請。
drop_boats.loc[drop_boats['Currency'].eq('EUR'), 'Price'] *= 1.10
uj5u.com熱心網友回復:
完成您想要的一種方法是這樣的:
for index, row in drop_boats.iterrows():
if row['Currency'] == 'EUR':
drop_boats.loc[index, 'Price'] = row['Price'] * 1.10
您可以相應地添加其他條件...
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/451171.html
