Pandas DataFrame:重组以下 .groupby()

Pandas DataFrame: Reorganise following .groupby()

我在看 NFT returns。 我有一个包含特定 ID 重复交易的数据集:

df_new = RSR.reset_index(drop=True)
print(df_new.head())

Here is the output:
 
   Asset ID     Collection        Date  Transaction price (USD)
0  10302582           axie  29/01/2020                   3.1159
1  10302582           axie  29/01/2020                   2.4535
2  10406110  cryptokitties  07/01/2020                   1.4192
3  10406110  cryptokitties  22/01/2020                   0.8415
4  10424431           axie  02/01/2020                   1.5289
...

单个 ID 的交易数量在 2 到 n 之间。

我正在尝试将输出显示在随附的图片中: link to ideal output.

基本上,每个完成的交易我都有一行,所以我可以计算 returns。

当只有两个交易时,我设法通过使用

达到非常相似的输出
c = df_new["Asset ID"]
RSR_clean = df_new.set_index([c, df_new.groupby(c).cumcount() + 1]).unstack().sort_index(1, 1)

The output is:

        Asset ID             Collection        Date Transaction price (USD)  \
                1                      1           1                       1    
Asset ID                                                                        
10302582  10302582                   axie  29/01/2020                  3.1159   
10406110  10406110          cryptokitties  07/01/2020                  1.4192   
10424431  10424431                   axie  02/01/2020                  1.5289   
1060112,  1060112,          cryptokitties  02/01/2020                 15.6885   
1092364,  1092364,                   axie  14/01/2020                165.9554   
...            ...                    ...         ...                     ...   
919066,   919066,           cryptokitties  10/01/2020                  1.3781   
9533256,  9533256,  cryptovoxel-wearables  21/01/2020                  0.8485   
971380,   971380,           cryptokitties  09/01/2020                 20.8469   
987084,   987084,           cryptokitties  03/01/2020                 16.1089   
992882,   992882,           cryptokitties  02/01/2020                 15.0981   

          Asset ID             Collection        Date Transaction price (USD)  \
                2                      2           2                       2    
Asset ID                                                                        
10302582  10302582                   axie  29/01/2020                  2.4535   
10406110  10406110          cryptokitties  22/01/2020                  0.8415   
10424431  10424431                   axie  14/01/2020                  3.1532   
1060112,  1060112,          cryptokitties  07/01/2020                 27.5083   
1092364,  1092364,                   axie  14/01/2020                165.9554   

N.B。实际上,这是每个资产 ID 的列集合。

然而,当每项资产的交易超过 2 笔时,我找不到办法。使用我当前的代码,它们只是作为新列添加。

我想要实现的是让第 3、4、5 等事务每次都成为一个新行。 在这些新行中,第3和第4列应该是之前的交易信息。

你知道我如何实现这种布局吗? 非常感谢!

如果我理解这个问题,您有一个交易数据框,其中每个资产(“资产 ID”列)都有两行或更多行数据。您想要按时间顺序连接所有与单个资产关联的后续数据行,以找出交易的 returns。

我认为您需要创建一个从 1 到 n-1 的“交易 ID”列,然后您将自行加入该列。把你的代码,这样的事情应该工作

df = RSR.reset_index(drop=True)

# Sort the data read for processing
df.sort_values(["Asset ID", "Date"], inplace=True)

# Add a Transaction ID column for the purchase
df["Transaction ID"] = df.groupby(["Asset ID"])["Transaction ID"].cumcount()

# Create a copy of the DataFrame for joining
df_sell = df.copy()

# Bump the Transaction ID for the sale
df_sell["Transaction ID"] = df_sell["Transaction ID"] + 1

# Join the Two DataFrames
df = df.merge(df_sell, on=["Asset ID", "Transaction ID"], suffixes=(" Buy", " Sell"))

这应该会像您预期的那样输出类似的 table,但它会有 1 和 2,但所有列都会有“买入”或“卖出”后缀。