我有这样的代码,它从字典创建数据帧,但输出与预期的不同.我给出了下面的代码和步骤

options_pnl={'BTC': {'Pnl(since 6pm)': Decimal('7831.52228528'),
  'Pnl(4 hr)': Decimal('2930.47450133'),
  'Pnl(1 hr)': Decimal('1416.81306308'),
  'Volume(since 6pm)': Decimal('24509290.62181862'),
  'Volume(4 hr)': Decimal('4504202.83422724'),
  'Volume(1 hr)': Decimal('1067850.01837278')},
 'ETH': {'Pnl(since 6pm)': Decimal('387.87564823'),
  'Pnl(4 hr)': Decimal('-349.14871930'),
  'Pnl(1 hr)': Decimal('656.74824550'),
  'Volume(since 6pm)': Decimal('10700784.53262117'),
  'Volume(4 hr)': Decimal('1968872.36761706'),
  'Volume(1 hr)': Decimal('778937.22275036')}}
options_pnl_df = pd.DataFrame.from_dict(options_pnl)
options_pnl_df = options_pnl_df.astype('int64')
options_pnl_df = options_pnl_df.transpose()
display(options_pnl_df)

输出如下:

     Pnl(since 6pm) Pnl(4 hr)   Pnl(1 hr)   Volume(since 6pm)   Volume(4 hr)    Volume(1 hr)
BTC  7831   2930    1416    24509290    4504202 1067850
ETH  387    -349    656 10700784    1968872 778937

我需要改变 struct ,从上面的输出到下面给出的输出

                         PNL    Volume
BTC since 6pm       7831       24509290
BTC since last 4 hr 2930       4504202
BTC last 1 hr       1416       1067850
ETH since 6pm       387        10700784
ETH since last 4 hr -349       1968872  
ETH last 1 hr       656        778937

try 过

data = []

for asset, values in options_pnl.items():
    for interval, metrics in values.items():
        metric_type, metric_interval = interval.split('(')
        metric_interval = metric_interval.rstrip(')')  # remove trailing ')'
        pnl_value = metrics if metric_type == 'Pnl' else 0
        volume_value = metrics if metric_type == 'Volume' else 0
        data.append([asset, metric_interval, pnl_value, volume_value])

columns = ['Asset', 'Type', 'PNL', 'Volume']
options_pnl_df = pd.DataFrame(data, columns=columns)

print(options_pnl_df)

但是没有得到想要的输出,有人能从给定的字典中得到想要的输出吗

推荐答案

您可以将当前列索引转换为MultiIndex,然后将结果转换为stack:

options_pnl_df.columns = pd.MultiIndex.from_arrays(options_pnl_df
                             .columns.str.extract(r'(\w+)\s*\(([\w\s]+)', expand=True)
                             .T
                             .values
                         )
options_pnl_df = options_pnl_df.stack()

输出:

                Pnl    Volume
BTC 1 hr       1416   1067850
    4 hr       2930   4504202
    since 6pm  7831  24509290
ETH 1 hr        656    778937
    4 hr       -349   1968872
    since 6pm   387  10700784

Python相关问答推荐

两极按组颠倒顺序

Python-Polars:如何用两个值的平均值填充NA?

如果在第一行之前不存在其他条件,如何获得满足口罩条件的第一行?

当pip为学校作业(job)安装sourcefender时,我没有收到匹配的分发错误.我已经try 过Python 3.8.10和3.10.11

过载功能是否包含Support Int而不是Support Int?

在使用Guouti包的Python中运行MPP模型时内存不足

如何计算列表列行之间的公共元素

GL pygame无法让缓冲区与vertextPointer和colorPointer一起可靠地工作

类型错误:输入类型不支持ufuncisnan-在执行Mann-Whitney U测试时[SOLVED]

如何标记Spacy中不包含特定符号的单词?

不理解Value错误:在Python中使用迭代对象设置时必须具有相等的len键和值

将图像拖到另一个图像

无法使用DBFS File API路径附加到CSV In Datricks(OSError Errno 95操作不支持)

使用密钥字典重新配置嵌套字典密钥名

cv2.matchTemplate函数匹配失败

如何保持服务器发送的事件连接活动?

* 动态地 * 修饰Python中的递归函数

matplotlib + python foor loop

如何使用使用来自其他列的值的公式更新一个rabrame列?

重置PD帧中的值