使用数学方程 Pandas Python 创建表格
根据下面的字典,我想为 META 和 BABA 的所有独特股票创建一个新的 DataFrame 函数。买入和卖出的数量对应于符号的买入和卖出。
我还想制作一个跟踪该股票的收益/损失
的专栏,它将从卖出价值中减去买入价值(买入 - 卖出),例如 (2360.15-2160.36) + (1897-1936.2)
由于 META 在 2 个不同的场合被买卖两次,所以价值就是这样。
最后但并非最不重要的一点是,我想通过公式 (Buy-Sell)/Buy * 100 计算增益/损失百分比,因此对于 META,公式如下 ((2360.15-2160.36)/2360.15 + (1897-1936.2)/1897)* 100)
。我如何修改下表以获得预期的输出?
import pandas as pd
a = {'Date': {0: '2/4/2022 1:33:40 PM', 1: '2/7/2022 3:09:46 PM', 2: '2/11/2022 9:35:44 AM', 3: '2/14/2022 2:55:33 PM', 4: '2/15/2022 3:55:33 PM', 5: '2/15/2022 9:15:33 PM'},
'TransactionType': {0: 'Buy', 1: 'Buy', 2: 'Sell', 3: 'Sell', 4: 'Buy', 5:'Sell'},
'Symbol': {0: 'META', 1: 'BABA', 2:'META', 3: 'BABA',4: 'META', 5: 'META' },
'Price': {0: 12.79, 1: 116.16, 2: 12.93, 3: 121.82, 4: 13.55, 5:13.83},
'Amount': {0: -2366.15, 1: -2439.36, 2: -2160.0, 3: 2558.22, 4:-1897, 5:1936.2}}
print((2360.15-2160.36) + (1897-1936.2))
print(2439.36-2558.22)
print(((2360.15-2160.36)/2360.15 + (1897-1936.2)/1897)* 100)
print(((2439.36-2558.22)/2439.36) * 100)
当前数据表:
预期输出:
Based on the dictionary below I want to create a new DataFrame function of all of the unique stocks being META and BABA. The Number of Buys and Sells corresponds to the Buying and selling of a symbol.
I would also like to make a column that tracks the Gains/Loss
of that stock that being it will subtract the buy value from sell value (Buy - Sell) like (2360.15-2160.36) + (1897-1936.2)
since META was bought and sold twice on 2 different occasions the value is gonna be like that.
Last but not least I would like to make the % Gain/Loss that is calculated by the equation (Buy-Sell)/Buy * 100 so for the META the equation would be like((2360.15-2160.36)/2360.15 + (1897-1936.2)/1897)* 100)
. How would I be able to modify the table below to get the expected Outputs?
import pandas as pd
a = {'Date': {0: '2/4/2022 1:33:40 PM', 1: '2/7/2022 3:09:46 PM', 2: '2/11/2022 9:35:44 AM', 3: '2/14/2022 2:55:33 PM', 4: '2/15/2022 3:55:33 PM', 5: '2/15/2022 9:15:33 PM'},
'TransactionType': {0: 'Buy', 1: 'Buy', 2: 'Sell', 3: 'Sell', 4: 'Buy', 5:'Sell'},
'Symbol': {0: 'META', 1: 'BABA', 2:'META', 3: 'BABA',4: 'META', 5: 'META' },
'Price': {0: 12.79, 1: 116.16, 2: 12.93, 3: 121.82, 4: 13.55, 5:13.83},
'Amount': {0: -2366.15, 1: -2439.36, 2: -2160.0, 3: 2558.22, 4:-1897, 5:1936.2}}
print((2360.15-2160.36) + (1897-1936.2))
print(2439.36-2558.22)
print(((2360.15-2160.36)/2360.15 + (1897-1936.2)/1897)* 100)
print(((2439.36-2558.22)/2439.36) * 100)
Current data table:
Expected Output:
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
(i) 我们可以使用
groupby
+count
+unstack
来获取每个“Symbol”的买入和卖出数量(ii) 我们可以使用
eq
+groupby
+cumsum
创建买卖对;然后groupby
这些对以及“符号”以获得收益/损失
和“百分比变化”列:输出:
请注意,我在这里得到的数字与数字不同您获得了预期的输出,因为您提供的输入 DataFrame 与您在分析中使用的 DataFrame 不同。
(i) We could use
groupby
+count
+unstack
to get the number of buys and sells for each "Symbol"(ii) We could create pairs from buy-sells using
eq
+groupby
+cumsum
; thengroupby
these pairs along with "Symbols" to get theGains/Losses
and "Percentage change" columns:Output:
Note that the number I get here are different from the numbers you have in the expected output because the input DataFrame you supplied is different from the DataFrame you used in your analysis.