【问题标题】:Pandas function explode does not work on this DataSeriesPandas 函数 explode 不适用于此 DataSeries
【发布时间】:2021-05-15 17:40:30
【问题描述】:

pandas 的 explode 函数不会像它应该的那样将对象元素放入行中。

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()

fundingdataMap 数据系列

https://www.pythonanywhere.com/user/armaniallie93/files/home/armaniallie93/fundingdataMap.txt

输出

data    [0.07280400000000001, 0.013058, 0.01, 0.01, 0....
Name: dataMap, dtype: object

在将列元素设置为字符串并切片我想要的部分后,没有错误,但它仍然不能正确产生分解功能。有什么原因吗?

【问题讨论】:

  • 请发布您的数据

标签: json pandas dataframe python-requests explode


【解决方案1】:

错误的原因很简单。你有一本字典,你想把它炸开,但这是行不通的。

#Removing the first row with dictionary
df.iloc[1:].explode('data')

#Without removing first row
df.explode('data')

您将不得不接受有关如何将此字典转换为列表的电话。这需要lambda 函数。

【讨论】:

  • 感谢您的帮助。参考代码,我能够将 DataFrame 转换为对象,定义列,将元素转换为字符串并切片我想要的元素。爆炸函数不会输出错误,但列不会按我想要的方式爆炸。对此有何见解?
  • "I was able to convert the DataFrame into an object, define a column, convert the elements to a string and slice the elements I wanted. The explode function does not output an error but the column doesn't explode the way I want to" - 你能更新你的问题来描述这个吗?
  • 好吧,我明白你的看法,它仍然是一本字典,这可能就是为什么在没有错误之后仍然没有正确爆炸的原因。您将如何应用 lambda 函数将其正确转换为具有正确索引的列表?
  • 您必须发布更多数据才能让我提出建议。我需要您数据中的字典和列表的完整示例。
  • 请在问题中发布您的数据以供我参考。几个完整的例子就可以了。
猜你喜欢
  • 2017-09-14
  • 1970-01-01
  • 2022-12-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-10-01
  • 2022-11-22
相关资源
最近更新 更多