- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个数据帧,即 df1 和 df2。我想对 df2 中的“Amount_Dollar”列执行操作。基本上在 df1 中,我有历史货币数据,并且我想根据 df2 中给定的货币和 Amount_Dollar 执行日期操作,以计算 df2 中 New_Amount_Dollar 列的值。
对于“货币”== [AUD、BWP],我们需要将 Amount_Dollar 乘以相应日期的相应货币值。
如果 df1 中没有任何货币,则不要对“Amount_Dollar”执行任何操作(意味着采用相同的值)
对于其他可用货币,我们需要将 Amount_Dollar 除以相应日期的相应货币值(value)。
例如,在 df2 中,我的第一种货币为澳元,日期 = '01-01-2019',因此我想计算 New_Amount_Dollar 值,使得
New_Amount_Dollar = Amount_Dollar*来自 df1 的澳元值,即 New_Amount_Dollar = 19298*98 = 1891204
另一个例子,在 df2 中,我有第三种货币作为 Date = '03-01-2019 的 COP,所以我想计算 New_Amount_Dollar 值,使得
New_Amount_Dollar = Amount_Dollar/df1 的 COP 值,即 New_Amount_Dollar = 5000/0.043 = 116279.06
import pandas as pd
data1 = {'Date':['01-01-2019', '02-01-2019', '03-01-2019',
'04-01-2019','05-01-2019'],
'AUD':[98, 98.5, 99, 99.5, 97],
'BWP':[30, 31, 33, 32, 31],
'CAD':[0.02, 0.0192, 0.0196, 0.0196, 0.0192],
'BND':[0.99, 0.952, 0.970, 0.980, 0.970],
'COP':[0.05, 0.047, 0.043, 0.047, 0.045]}
df1 = pd.DataFrame(data1)
data2 = {'Date':['01-01-2019', '02-01-2019', '03-01-2019', '04-01-2019','05-01-2019'],
'Currency':['AUD','AUD','COP','NZD','BND'],
'Amount_Dollar':[19298, 19210, 5000, 200, 2300],
'New_Amount_Dollar':[0,0,0,0,0]
}
df2 = pd.DataFrame(data2)
df1
Date AUD BWP CAD BND COP
0 01-01-2019 98.0 30 0.0200 0.990 0.050
1 03-01-2019 98.5 31 0.0192 0.952 0.047
2 04-01-2019 99.0 33 0.0196 0.970 0.043
3 05-01-2019 99.5 32 0.0196 0.980 0.047
4 06-01-2019 97.0 31 0.0192 0.970 0.045
5 09-01-2019 100.0 20 0.2000 0.230 0.023
df2
Date Currency Amount_Dollar New_Amount_Dollar
0 01-01-2019 AUD 19298 0
1 02-01-2019 AUD 19210 0
2 03-01-2019 COP 5000 0
3 04-01-2019 NZD 200 0
4 07-01-2019 BND 2300 0
预期结果
Date Currency Amount_Dollar New_Amount_Dollar
0 01-01-2019 AUD 19298 1891204.00
1 02-01-2019 AUD 19210 1892185.00
2 03-01-2019 COP 5000 116279.06
3 04-01-2019 NZD 200 200.00
4 05-01-2019 BND 2300 2371.13
最佳答案
首先,unstack df1得到一列所有FX,稍后将用于连接:
df1.set_index('Date', inplace=True)
df1 = pd.DataFrame(df1.unstack(), columns = ['FX'])
然后加入 df2。为 CAD 和 BWP 添加 mask ,取 CAD/BWP 的倒数。然后进行列操作以获得新的金额。
df2 = df2.merge(df1, left_on = ['Currency', 'Date'], right_index = True, how = 'left').fillna(1)
df2['mask'] = df2['Currency'].isin(['AUD', 'BWP'])
df2.loc[df2['mask'], 'FX'] = 1/df2.loc[df2['mask'], 'FX']
df2['New_Amount_Dollar'] = df2['Amount_Dollar'] / df2['FX']
关于python - 使用 pandas 对多个映射货币列进行操作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58790545/
现在已知如下问题,并告诉你这题可以用网络流来解决,你该怎么做,该怎么建出网络流的模型? 一些前提: 显然可以发现绝不可能走横向向左的边,但可能走竖向向上的边(如下图) 那么图其实就是这样的:
条件: 我们需要我们 Magento 网站的默认显示货币是美元,Paypal(基础货币)也需要是美元(因为我们需要客户以美元支付)。 我们的 Fedex a/c 是在新加坡开设的,结果发现运费是以新加
根据 currency_str 和 created_date_time 合并两个数据帧(xrate 和 df)时遇到问题 display(xrate.info()) Int64Index: 1611
我必须匹配像 这样的值 € 6.483,00 或类似的值 18,50% 或者,再次, +65,86 % 在我起草的 Javascript 函数中: function(s) { return /^
我正在尝试将货币金额解析并存储为 BigDecimal 值。我得到了相关货币的区域设置,在大多数情况下它工作正常,但当货币是哥斯达黎加科朗时我得到了意想不到的结果。 我的哥斯达黎加客户告诉我,典型的货
在当前版本的 Excel(Office 365/2019 年 2 月)中获得近似每日外汇汇率的最简单的免费方法是什么。 我对历史数据不感兴趣,我只想要从货币 X 到货币 Y 的最后已知日汇率,并且只在
我在 spree 时遇到了麻烦,不知道如何处理。 我无法更改主要货币。 我该怎么做? 最佳答案 它在 Spree 2.0.0 中更容易,而且显然它也可以在以前的 spree 版本中工作。 转到您的 c
在我的网络应用程序中,我有一个名为“预算”的输入字段,用户可以在其中输入项目的建议预算。我需要创建一个屏蔽输入,以便在用户在输入字段中输入时自动将输入的金额转换为以下格式: 1000 10 000 1
我正在从数据库返回一个字符串,但由于数据库的编码方式,一些英镑符号 (£) 被问号取代。我想恢复井号,但不替换字符串中真正的问号。我已经设法编写了一个正则表达式来测试问号后跟数字的组合,但我不确定如何
我在使用亚马逊销售 API 时遇到问题。我在 amazon.co.uk 有一个帐户,可以正常发送产品。我在 amazon.de 有一个新帐户,除了货币外,一切都很好。 我有一个零售价为 10 英镑 (
Pharo 有什么方法可以将数字转换为单词。 例如:1200 = 一千二百而已。 实现起来并不难,只是想知道。 最佳答案 Integer>>asWords会这样做。 1200 asWords返回 'o
我有一个简单的单页网络应用程序。它从两个不同的 API 获取货币数据(以美元为单位),在成功检索后,promise 被解决,一些计算得出 GBP/Bitcoin 汇率。 我正试图找到一种干净的方法,然
我有以下简单的计算,它将两个值加在一起。这些值与使用“R”作为前缀标识的兰特(南非货币)有关。 function calculate() { var A = parseFloat(docume
使用 JavaScript,什么是将点替换为逗号的正确方法(对于欧盟货币),例如: 2000.65 将是 2000,65 而不是 2,000.65 39.20 将是 39,20 我不确定 cost.r
我在文本框中显示带有 NumberFormat 的货币符号 NumberFormat numberFormat = NumberFormat.getSimpleCurrencyFo
我已经设法从非接触式阅读器读取了一个交易事件,使用 现在我的 Activity 打开了,我被困在那个点上,因为我认为我的 Intent 中会有一些数据,比如 amou
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 7 年前。 Improve th
这个问题在这里已经有了答案: Why not use Double or Float to represent currency? (16 个答案) 关闭 3 年前。 我的应用程序中的一些计算有问题
我使用 Jruby(反正就是 ruby,在 jvm 下运行 :D )和马拉松测试(一个 java swing 应用程序),我在处理货币数字时遇到了一些麻烦。 我不使用 Rails(不知道我是否可以
我正在尝试删除 、 或 之后的尾随零。 以一种货币表示,仍然保留货币符号。例如,€90.00 到 €90、90.00€ 到 90€ 和 €90.33 到 €90.33。 示例如下: $('.produ
我是一名优秀的程序员,十分优秀!