numpy 如何在pandas中改变框架结构

ubof19bj  于 11个月前  发布在  其他
关注(0)|答案(1)|浏览(129)

我从两个这样的字典中创建了一个框架

import pandas as pd
summary={'BTC': {'DAE': -11661300, 'Vega': -661, 'Theta': 109, 'Gamma': -32391},
 'ETH': {'DAE': -533985, 'Vega': -227, 'Theta': 518, 'Gamma': -289779}}
summary_df = pd.DataFrame.from_dict(summary)
summary_df = summary_df.transpose()
display(summary_df)

greeks_6_pm= {'BTC': {'DAE': -11725765, 'Vega': -567, 'Theta': 79, 'Gamma': 85512},
 'ETH': {'DAE': -676607, 'Vega': -15, 'Theta': 186, 'Gamma': -78159}}

change_of_greeks_df = pd.DataFrame(summary).sub(pd.DataFrame(greeks_6_pm)).transpose().add_prefix("△")
pnl_summary = pd.merge(summary_df, change_of_greeks_df, left_index=True, right_index=True)

字符串
我得到的输出像

DAE         Vega    Theta   Gamma   △DAE    △Vega   △Theta  △Gamma
BTC -11661300   -661    109    -32391   64465   -94 30  -117903
ETH -533985    -227     518   -289779   142622  -212    332 -211620


但我需要输出像

Currency     Dae    Vega    theta   gamma
BTC     -11661300   -661    109    -32391
△BTC    64465   -94 30  -117903         
ETH    -533985     -227     518   -289779           
△ETH    142622  -212    332 -211620


尝试了

pnl_summary = pd.concat([summary_df, change_of_greeks_df], axis=0, keys=['', 'Changed']).swaplevel(axis=0)
display(pnl_summary)


但是得到的输出

DAE Vega    Theta   Gamma   △DAE    △Vega   △Theta  △Gamma
BTC     -11,661,300 -661    109 -32,391 nan nan nan nan
ETH     -533,985    -227    518 -289,779    nan nan nan nan
BTC Changed nan nan nan nan 64,465  -94 30  -117,903
ETH Changed nan nan nan nan 142,622 -212    332 -211,620


如何在JavaScript中获得所需的输出

vojdkbi0

vojdkbi01#

给您:

back2greek = pnl_summary[change_of_greeks_df.columns].set_axis(summary_df.columns, axis=1)
back2greek = back2greek.set_index(back2greek.index.astype(str) + '△')
df = pd.concat([pnl_summary[summary_df.columns], back2greek]).sort_index()
df

           DAE  Vega  Theta   Gamma
BTC  -11661300  -661    109  -32391
BTC△     64465   -94     30 -117903
ETH    -533985  -227    518 -289779
ETH△    142622  -212    332 -211620

字符串

相关问题