numpy 如何更改pandas中的行和列

5cg8jx4n  于 11个月前  发布在  其他
关注(0)|答案(2)|浏览(159)

我有这样的代码,它从字典中创建一个框架,但输出与预期的不同。

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


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

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


尝试了

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)


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

iecba09b

iecba09b1#

您可以将当前列索引转换为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

6qqygrtg

6qqygrtg2#

据我所知,你有两个类别(BTC和ETH),并希望使用相同的时间间隔来创建索引。这里有一个解决方法。

index_list = []
options_pnl_dict = {}
for category, values in options_pnl.items():
  for k,v in values.items():
    temp = k.split("(")
    index = f"{category} ({temp[1].strip()[:-1]})"
    if index not in index_list:
      index_list.append(index)
    
    if temp[0] in options_pnl_dict.keys():
      options_pnl_dict[temp[0]].append(v)
    else:  
      options_pnl_dict[temp[0]] = [v]

字符串
现在你有了索引和字典。

pd.DataFrame(options_pnl_dict, index=index_list)
PNL             Volume
BTC (since 6pm)        7831.52228528  24509290.62181862
BTC (since last 4 hr)  2930.47450133   4504202.83422724
BTC (last 1 hr)        1416.81306308   1067850.01837278
ETH (since 6pm)         387.87564823  10700784.53262117
ETH (since last 4 hr)  -349.14871930   1968872.36761706
ETH (last 1 hr)         656.74824550    778937.22275036

的数据

相关问题