我有一个通过合并两个看起来像这样的数据框而创建的数据框:
Salary Amount: Salary Currency: Country Origin: Exchange Rate:
55000.0 EUR EUR 1.195826
200000.0 USD USD 1.000000
130000.0 GBP GBP 1.324188
数据框中还有更多货币,这只是一个例子。我想将薪水金额乘以汇率GBP,以使数据框中的每一行都具有英镑(GBP)。我还想打印出英镑的最高和中位数工资。我想我必须将薪金数额与汇率相乘,但是我不确定。感谢您的帮助。
因此,如果要将所有3行都乘以第三行(GBP)中的“汇率”,可以尝试以下操作:
df = pd.DataFrame([[55000.0,'EUR','EUR',1.195826],
[200000.0,'USD','USD',1.000000],
[130000.0,'GBP','GBP',1.324188]],
columns=['Salary Amount:','Salary Currency:','Country Origin:','Exchange Rate:'])
exchange_rate_GBP = df[df['Salary Currency:']=='GBP']['Exchange Rate:'].values
df['converted'] = df['Salary Amount:']*exchange_rate_GBP
print(df)
Salary Amount: Salary Currency: Country Origin: Exchange Rate: converted
0 55000.0 EUR EUR 1.195826 72830.34
1 200000.0 USD USD 1.000000 264837.60
2 130000.0 GBP GBP 1.324188 172144.44
获得均值和最大值:
mean_salary_GBP = df['converted'].mean()
max_salary_GBP = df['converted'].max()
print(mean_salary_GBP, max_salary_GBP)
输出:
169937.46 264837.6