简体   繁体   English

ValueError:DataFrame 构造函数未正确调用! 当将列表中的字典覆盖到 pandas dataframe

[英]ValueError: DataFrame constructor not properly called! when coverting dictionaries within list to pandas dataframe

I want to convert a list of dictionaries to a pandas dataframe, however, I got ValueError: DataFrame constructor not properly called!我想将字典列表转换为 pandas dataframe,但是,我得到了ValueError: DataFrame constructor not properly called!

Below is an example and how I got the data:下面是一个示例以及我如何获取数据:

import requests
import pandas as pd

# Send an HTTP GET request to the URL
response = requests.get(url)

# Decode the JSON data into a dictionary
scrapped_data = response.text

Content of response.text is: response.text的内容是:

[{"id":123456,"date":"12-12-2022","value":37},{"id":123456,"date":"13-12-2022","value":38}]

I want to convert it to a dataframe format like the following:我想将其转换为 dataframe 格式,如下所示:

id ID date日期 value价值
123456 123456 12-12-2022 12-12-2022 37 37
123456 123456 13-12-2022 13-12-2022 38 38

I tried the following methods:我尝试了以下方法:

df = pd.DataFrame(scrapped_data)

df = pd.DataFrame_from_dict(scrapped_data)

df = pd.DataFrame(scrapped_data, orient='columns')

all got the same value errors.都有相同的值错误。

I also tried:我也试过:

df = pd.json_normalize(scrapped_data)

but got NotImplementedError但得到了NotImplementedError

The type for scrapped_data is string format scrapped_data 的类型是字符串格式

Thanks for your help, let me know if you have any questions感谢您的帮助,如果您有任何问题,请告诉我

As you said, scrapped_data is a string then you need to convert it into a dictionary (with the method loads from the json library for example).如您所说, scrapped_data 是一个字符串,然后您需要将其转换为字典(例如,使用从 json 库加载的方法)。

If scrapped_data = '[{"id":"123456","date":"12-12-2022","value":"37"}, {"id":"123456","date":"13-12-2022","value":"38"}]', then you can just do df = pd.DataFrame(scrapped_data).如果 scrapped_data = '[{"id":"123456","date":"12-12-2022","value":"37"}, {"id":"123456","date":"13 -12-2022","value":"38"}]',那么你可以只做 df = pd.DataFrame(scrapped_data)。

One reason for receiving this error from pandas is providing str as data.pandas收到此错误的原因之一是提供str作为数据。 I think your data come as str , If it is the case then Try this:我认为您的数据来自str ,如果是这样,请尝试以下操作:

import json
import pandas as pd

orignal_data='[{"id":"123456","date":"12-12-2022","value":"37"}, {"id":"123456","date":"13-12-2022","value":"38"}]'
scraped_data = json.loads(orignal_data)
df = pd.DataFrame(data=scraped_data)
df

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 ValueError:未正确调用 DataFrame 构造函数! 与熊猫 - ValueError: DataFrame constructor not properly called! with pandas 将多个字典附加到Pandas数据框:错误的数据框构造函数未正确调用? - Appending multiple dictionaries to Pandas dataframe: Error DataFrame constructor not properly called? ValueError DataFrame构造函数未正确调用 - ValueError DataFrame constructor not properly called ValueError: DataFrame 构造函数未正确调用 - ValueError: DataFrame constructor not properly called 将列表列表转换为数据框时出现“ ValueError:DataFrame构造函数未正确调用!” - “ValueError: DataFrame constructor not properly called!” while converting list of lists to dataframe 给定系列列表时,熊猫“未正确调用 DataFrame 构造函数” - pandas "DataFrame constructor not properly called" when given a list of Series ValueError: DataFrame 构造函数未正确调用 (Databricks/Python) - ValueError: DataFrame constructor not properly called (Databricks/Python) 无法修复“ValueError:未正确调用 DataFrame 构造函数!” - Unable to fix “ValueError: DataFrame constructor not properly called!” 接收 ValueError: DataFrame 构造函数未正确调用 - Receiving ValueError: DataFrame constructor not properly called ValueError:未正确调用 sarima 的 DataFrame 构造函数 - ValueError: DataFrame constructor not properly called for sarima
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM