2021年2月11日星期四

Pandas function explode does not work on this DataSeries

The pandas explode function doesn't drop the object elements into rows like it should.

import pandas as pd  import requests  import io  from pandas.io.json import json_normalize    response = requests.request("GET", url, headers=headers, data = payload)  response_text = response.text.encode('utf8')  fundingRate = pd.read_json(response_text,orient='columns',typ='frame')  fundingC = pd.DataFrame(fundingRate['data'])  fundingC = fundingC.T  fundingC = fundingC.astype(object)  fundingdataMap = fundingC['dataMap']  fundingdataMap = fundingdataMap.astype(str)  fundingdataMap = fundingdataMap.str.slice(start=10)  fundingdataMap.explode()  

fundingRate DataFrame

           code      msg                                               data  \  dataMap       0  success  {'Huobi': [0.056896, 0.01, 0.10996600000000001...     dateList      0  success  [1597420800000, 1597449600000, 1597478400000, ...     priceList     0  success  [438.37, 438.98, 438.3, 436.22, 433.21, 427.63...     

output

KeyError                                  Traceback (most recent call last)  <ipython-input-556-074d34b73435> in <module>        9 #fundingC['date'].apply(', '.join)       10 #fundingdataMap = fundingdataMap.str.slice(start=10)  ---> 11 fundingdataMap.explode()       12 print(fundingRate)    /usr/lib/python3.8/site-packages/pandas/core/series.py in explode(self)     3698             return self.copy()     3699   -> 3700         values, counts = reshape.explode(np.asarray(self.array))     3701      3702         result = Series(values, index=self.index.repeat(counts), name=self.name)    pandas/_libs/reshape.pyx in pandas._libs.reshape.explode()    KeyError: 0  

any insight to why and if there is another set of methods?

https://stackoverflow.com/questions/66163926/pandas-function-explode-does-not-work-on-this-dataseries February 12, 2021 at 06:31AM

没有评论:

发表评论