pandas 嵌套字典到数据框

camsedfj  于 2023-01-01  发布在  其他
关注(0)|答案(1)|浏览(113)

有人能帮忙吗

msg = {'e': 'kline',        
       'E': 1672157513375, 
       's': 'BTCUSDT', 
       'k': {
             't': 1672157460000, #REQUIRE, CONVERT MS TO DATETIME,
                                 #RENAME AS TIME, AS INDEX
             'T': 1672157519999, 
             's': 'BTCUSDT', 
             'i': '1m', 
             'f': 2388965371, 
             'L': 2388969270, 
             'o': '16787.32000000', #REQUIRE RENAME AS OPEN
             'c': '16783.23000000', #REQUIRE RENAME AS CLOSE
             'h': '16789.41000000', #REQUIRE RENAME AS HIGH
             'l': '16782.69000000', #REQUIRE RENAME AS LOW
             'v': '149.27507000',   #REQUIRE RENAME AS VOLUME
             'n': 3900, 
             'x': False, 
             'q': '2505669.98288240', 
             'V': '59.70465000', 
             'Q': '1002207.92308370', 
             'B': '0'
            }
}

时间= k(t),日期时间
开= k(o),d型浮点
高= k(h),d型浮点
低= k(l),d型浮点
闭合= k(c),d型浮点
体积= k(v),d型浮子
给予as,k(t)将该毫秒转换为日期时间,并将give转换为索引语言python
我尝试:

def getdata(msg):
    frame = pd.DataFrame(msg)
    #DONT UNDERSTOOD
    frame = frame.loc[frame['k']['t'],frame['k']['t'],frame['k']['t'],
                      frame['k']['t'],frame['k']['t'],frame['k']['t']]
    #SOME UNDERSTOOD
    frame.columns = ["Time","Open","High","Low","Close","Volume"]
    frame.set_index("Time",inplace=True)
    frame.index = pd.to_datetime(frame.index,unit='ms')
    frame = frame.astype(float)
    return frame

getdata(msg)

要求输出:

Time                 Open     High     Low      Close   Volume

2022-12-27 16:11:00  16787.7  16789.4  16782.6  16783.2 149

〈3

wgx48brx

wgx48brx1#

使用json_normalize():

df = (pd
      .json_normalize(data=msg["k"])[["t", "o", "h", "l", "c", "v"]]
      .rename(columns={"t": "Time", "o": "Open", "h": "High", "l": "Low", "c": "Close", "v": "Volume"})
      )

df["Time"] = (
    pd.to_datetime(df["Time"], unit="ms")
    .dt.tz_localize("UTC")
    .dt.tz_localize(None)
    .dt.floor("S")
)
print(df)

输出:

Time            Open            High             Low           Close        Volume
0 2022-12-27 16:11:00  16787.32000000  16789.41000000  16782.69000000  16783.23000000  149.27507000

相关问题