Python 如何用这样的标题组织数据帧?
我的程序返回一个有多个层的字典。在Excel中绘制时,如下所示: Excel模板:Python 如何用这样的标题组织数据帧?,python,excel,pandas,database,dataframe,Python,Excel,Pandas,Database,Dataframe,我的程序返回一个有多个层的字典。在Excel中绘制时,如下所示: Excel模板: 代码将返回此词典(已组织该词典以帮助您可视化): 我希望在xlsx中保持数据帧的组织方式与上面的一样。只需两个步骤 pd.json\u normalize()从json生成数据帧 使用pd.MultiIndex.from_tuples() 样本输出 看见 {'Parity': 'BITCOIN / TETHERUS', '1m': {'Datas': {'Price': {'Initial Price': '5
代码将返回此词典(已组织该词典以帮助您可视化): 我希望在xlsx中保持数据帧的组织方式与上面的一样。只需两个步骤
pd.json\u normalize()
从json生成数据帧pd.MultiIndex.from_tuples()
{'Parity': 'BITCOIN / TETHERUS', '1m': {'Datas': {'Price': {'Initial Price': '58239.43', 'Final Price': '58236.50'}, 'Oscillators': {'Buy': 1, 'Neutral': 8, 'Sell': 2}, 'MovingAverages': {'Buy': 6, 'Neutral': 1, 'Sell': 8}}}, '5m': {'Datas': {'Price': {'Initial Price': '58239.43', 'Final Price': '58231.56'}, 'Oscillators': {'Buy': 0, 'Neutral': 9, 'Sell': 2}, 'MovingAverages': {'Buy': 13, 'Neutral': 1, 'Sell': 1}}}, '15m': {'Datas': {'Price': {'Initial Price': '58239.43', 'Final Price': '58231.56'}, 'Oscillators': {'Buy': 1, 'Neutral': 6, 'Sell': 4}, 'MovingAverages': {'Buy': 13, 'Neutral': 1, 'Sell': 1}}}, '1h': {'Datas': {'Price': {'Initial Price': '58239.43', 'Final Price': '58231.55'}, 'Oscillators': {'Buy': 2, 'Neutral': 7, 'Sell': 2}, 'MovingAverages': {'Buy': 14, 'Neutral': 1, 'Sell': 0}}}, '4h': {'Datas': {'Price': {'Initial Price': '58239.43', 'Final Price':'58231.56'}, 'Oscillators': {'Buy': 3, 'Neutral': 8, 'Sell': 0}, 'MovingAverages': {'Buy': 14, 'Neutral': 1, 'Sell': 0}}}, '1D': {'Datas': {'Price': {'Initial Price': '58239.43', 'Final Price': '58231.56'}, 'Oscillators': {'Buy': 2, 'Neutra
l': 9, 'Sell': 0}, 'MovingAverages': {'Buy': 14, 'Neutral': 1, 'Sell': 0}}}, '1W': {'Datas': {'Price': {'Initial Price': '58239.43', 'Final Price': '58230.02'}, 'Oscillators': {'Buy': 1, 'Neutral': 9, 'Sell': 1}, 'MovingAverages': {'Buy': 12, 'Neutral': 3, 'Sell': 0}}}}
js = {"Parity": "BITCOIN / TETHERUS", "1m": {"Datas": {"Price": {"Initial Price": "58239.43", "Final Price": "58236.50"}, "Oscillators": {"Buy": 1, "Neutral": 8, "Sell": 2}, "MovingAverages": {"Buy": 6, "Neutral": 1, "Sell": 8}}}, "5m": {"Datas": {"Price": {"Initial Price": "58239.43", "Final Price": "58231.56"}, "Oscillators": {"Buy": 0, "Neutral": 9, "Sell": 2}, "MovingAverages": {"Buy": 13, "Neutral": 1, "Sell": 1}}}, "15m": {"Datas": {"Price": {"Initial Price": "58239.43", "Final Price": "58231.56"}, "Oscillators": {"Buy": 1, "Neutral": 6, "Sell": 4}, "MovingAverages": {"Buy": 13, "Neutral": 1, "Sell": 1}}}, "1h": {"Datas": {"Price": {"Initial Price": "58239.43", "Final Price": "58231.55"}, "Oscillators": {"Buy": 2, "Neutral": 7, "Sell": 2}, "MovingAverages": {"Buy": 14, "Neutral": 1, "Sell": 0}}}, "4h": {"Datas": {"Price": {"Initial Price": "58239.43", "Final Price": "58231.56"}, "Oscillators": {"Buy": 3, "Neutral": 8, "Sell": 0}, "MovingAverages": {"Buy": 14, "Neutral": 1, "Sell": 0}}}, "1D": {"Datas": {"Price": {"Initial Price": "58239.43", "Final Price": "58231.56"}, "Oscillators": {"Buy": 2, "Neutral": 9, "Sell": 0}, "MovingAverages": {"Buy": 14, "Neutral": 1, "Sell": 0}}}, "1W": {"Datas": {"Price": {"Initial Price": "58239.43", "Final Price": "58230.02"}, "Oscillators": {"Buy": 1, "Neutral": 9, "Sell": 1}, "MovingAverages": {"Buy": 12, "Neutral": 3, "Sell": 0}}}}
df = pd.json_normalize(js).set_index("Parity")
df.columns=pd.MultiIndex.from_tuples([tuple(c.split(".")) for c in df.columns], names=["tenor","datas","type","measure"])
df = df.droplevel("datas",1)
tenor 1m 5m
type Price Oscillators MovingAverages Price Oscillators MovingAverages
measure Initial Price Final Price Buy Neutral Sell Buy Neutral Sell Initial Price Final Price Buy Neutral Sell Buy Neutral Sell
Parity
BITCOIN / TETHERUS 58239.43 58236.50 1 8 2 6 1 8 58239.43 58231.56 0 9 2 13 1 1