我正在尝试为DataFrame
,
exchange_rates = {'TRY': 3.4155, 'NZD': 1.3683, 'HUF': 254.11, 'RUB': 57.185,
'DKK': 6.1693, 'SEK': 7.9095, 'SGD': 1.337, 'CAD': 1.2087, 'JPY': 107.38,
'AUD': 1.2339, 'HRK': 6.1621, 'CHF': 0.94569, 'ILS': 3.5163, 'THB': 33.1,
'GBP': 0.75678, 'BGN': 1.6217, 'KRW': 1130.6, 'NOK': 7.7192, 'CZK': 21.641,
'ZAR': 12.862, 'PHP': 50.809, 'MYR': 4.1945, 'EUR': 0.82919, 'MXN': 17.693,
'CNY': 6.4615, 'HKD': 7.8076, 'RON': 3.8149, 'INR': 63.784, 'PLN': 3.5269,
'IDR': 13163.0, 'BRL': 3.0892}
unique_currencies = ['CAD', 'CHF', 'CNY', 'EUR', 'JPY', 'GBP', 'SGD']
for cur in unique_currencies:
cur_indices = df[df['currency'] == cur].index.tolist()
for row_index in cur_indices:
df.loc[row_index, 'common_amount'] = df.loc[row_index, 'amount'] / exchange_rates[cur]
因此,代码获取具有特定货币的行的索引,然后根据其汇率将值计算为“USD”到列common_amount
。
我想知道最好的方法是什么?
更新
示例DataFrame,
currency amount
50 CAD 410.85
51 CAD 1441.68
17625 JPY 2797856.0
17663 JPY 1440.0
16734 CNY 27840.00
54546 CNY 273269.53
17654 GBP 384.0
17655 GBP 526.0
16732 CHF 474.7
16733 CHF 195173.3
答案 0 :(得分:2)
我认为您需要按currency
dict
print (df['currency'].map(exchange_rates))
50 1.20870
51 1.20870
17625 107.38000
17663 107.38000
16734 6.46150
54546 6.46150
17654 0.75678
17655 0.75678
16732 0.94569
16733 0.94569
Name: currency, dtype: float64
df['common_amount1'] = df['amount'].div(df['currency'].map(exchange_rates))
print (df)
currency amount common_amount common_amount1
50 CAD 410.85 339.910648 339.910648
51 CAD 1441.68 1192.752544 1192.752544
17625 JPY 2797856.00 26055.652822 26055.652822
17663 JPY 1440.00 13.410318 13.410318
16734 CNY 27840.00 4308.597075 4308.597075
54546 CNY 273269.53 42291.964714 42291.964714
17654 GBP 384.00 507.412987 507.412987
17655 GBP 526.00 695.050081 695.050081
16732 CHF 474.70 501.961531 501.961531
16733 CHF 195173.30 206381.901046 206381.901046
创建的div
列除以 if (edt1 == null){
edt1.setText("");
}
:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:id="@+id/first_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="0.40"/>
<EditText
android:id="@+id/second_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:layout_weight="0.40"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" = "
android:layout_weight="0.10"/>
<TextView
android:id="@+id/result"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:layout_weight="0.20"/>
</LinearLayout>