使用数学方程 Pandas Python 创建表格

发布于 2025-01-10 08:04:10 字数 1418 浏览 0 评论 0原文

根据下面的字典,我想为 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:

enter image description here

Expected Output:

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情域 2025-01-17 08:04:10

(i) 我们可以使用 groupby + count + unstack 来获取每个“Symbol”的买入和卖出数量

(ii) 我们可以使用 eq + groupby + cumsum 创建买卖对;然后groupby这些对以及“符号”以获得收益/损失和“百分比变化”列:

out = df.groupby(['Symbol','TransactionType'])['TransactionType'].count().unstack().add_prefix('Number of ').add_suffix('s')
g = df.groupby(['Symbol', df['TransactionType'].eq('Buy').groupby(df['Symbol']).cumsum()])['Amount']
out['Gains/Losses'] = g.sum().groupby(level=0).sum()
out['Percentage change'] = g.pct_change().groupby(df['Symbol']).sum()
out = out.reset_index().rename_axis([None], axis=1)

输出:

  Symbol  Number of Buys  Number of Sells  Gains/Losses  Percentage change
0   BABA               1                1        118.86          -2.048726
1   META               2                2       -166.95          -3.933540

请注意,我在这里得到的数字与数字不同您获得了预期的输出,因为您提供的输入 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; then groupby these pairs along with "Symbols" to get the Gains/Losses and "Percentage change" columns:

out = df.groupby(['Symbol','TransactionType'])['TransactionType'].count().unstack().add_prefix('Number of ').add_suffix('s')
g = df.groupby(['Symbol', df['TransactionType'].eq('Buy').groupby(df['Symbol']).cumsum()])['Amount']
out['Gains/Losses'] = g.sum().groupby(level=0).sum()
out['Percentage change'] = g.pct_change().groupby(df['Symbol']).sum()
out = out.reset_index().rename_axis([None], axis=1)

Output:

  Symbol  Number of Buys  Number of Sells  Gains/Losses  Percentage change
0   BABA               1                1        118.86          -2.048726
1   META               2                2       -166.95          -3.933540

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文