code

panda read_json: "모든 스칼라 값을 사용하는 경우 인덱스를 통과해야 합니다."

starcafe 2023. 3. 14. 21:47
반응형

panda read_json: "모든 스칼라 값을 사용하는 경우 인덱스를 통과해야 합니다."

JSON 파일을 팬더와 함께 수입하는데 어려움이 있습니다.

import pandas as pd
map_index_to_word = pd.read_json('people_wiki_map_index_to_word.json')

다음과 같은 오류가 발생합니다.

ValueError: If using all scalar values, you must pass an index

파일 구조는 다음과 같이 단순해집니다.

{"biennials": 522004, "lb915": 116290, "shatzky": 127647, "woode": 174106, "damfunk": 133206, "nualart": 153444, "hatefillot": 164111, "missionborn": 261765, "yeardescribed": 161075, "theoryhe": 521685}

Coursera에 있는 Washington University of Coursera의 기계 학습 코스에서 나온 것입니다.파일은 여기서 찾을 수 있습니다.

해라

ser = pd.read_json('people_wiki_map_index_to_word.json', typ='series')

이 파일에는 값이 스칼라인 키 값 쌍만 포함됩니다.를 사용하여 데이터 프레임으로 변환할 수 있습니다.ser.to_frame('count').

다음과 같은 작업을 수행할 수도 있습니다.

import json
with open('people_wiki_map_index_to_word.json', 'r') as f:
    data = json.load(f)

데이터는 사전입니다.다음과 같이 데이터 프레임컨스트럭터에 전달할 수 있습니다.

df = pd.DataFrame({'count': data})

컬럼 베이스 형식을 제공하는 @ayhan communication으로 수행할 수 있습니다.

방법 1

또는 다음과 같이 오브젝트를 [ ](소스)로 둘러싸서 여러 값을 로드하고 기계학습 모델에 매트릭스를 사용할 계획일 때 편리한 행 형식을 제공할 수 있습니다.

df = pd.DataFrame([data])

방법 2

제 생각에 지금 일어나고 있는 일은 그 안에 있는 데이터가

map_index_to_word = pd.read_json('people_wiki_map_index_to_word.json')

json이 아닌 문자열로 읽히고 있습니다.

{"biennials": 522004, "lb915": 116290, "shatzky": 127647, "woode": 174106, "damfunk": 133206, "nualart": 153444, "hatefillot": 164111, "missionborn": 261765, "yeardescribed": 161075, "theoryhe": 521685}

사실

'{"biennials": 522004, "lb915": 116290, "shatzky": 127647, "woode": 174106, "damfunk": 133206, "nualart": 153444, "hatefillot": 164111, "missionborn": 261765, "yeardescribed": 161075, "theoryhe": 521685}'

문자열은 스칼라이므로 json으로 로드해야 합니다.다른 응답이 하고 있는 것과 같은 dict로 변환해야 합니다.

가장 좋은 방법은 줄에 json을 로드하여 dict로 변환하고 팬더에 로드하는 것입니다.

myfile=f.read()
jsonData=json.loads(myfile)
df=pd.DataFrame(data)
{
"biennials": 522004,
"lb915": 116290
}

df = pd.read_json('values.json')

pd.read_json이 예상하는 대로

{
"biennials": [522004],
"lb915": [116290]
}

특정 키에 대해 다음과 같은 오류를 반환합니다.

모든 스칼라 값을 사용하는 경우 인덱스를 전달해야 합니다.

이를 해결하려면 pd.read_json에서 'typ' arg를 지정합니다.

map_index_to_word = pd.read_json('Datasets/people_wiki_map_index_to_word.json', typ='dictionary')

최신 판다의 경우 0.19.0 이상일 경우 lines 매개 변수를 사용하여 True로 설정합니다.파일은 한 줄에 json 개체로 읽힙니다.

import pandas as pd
map_index_to_word = pd.read_json('people_wiki_map_index_to_word.json', lines=True)

다음 오류를 수정하면 특히 일부 json 파일의 값이 1개뿐일 때 발생합니다.

  1. ValueError: 모든 스칼라 값을 사용하는 경우 인덱스를 전달해야 합니다.
  2. JSONDecodeError: 예상 값: 1행 1열(char 0)
  3. 값 오류:후행 데이터

를 들어 cat values.json 입니다.

{
name: "Snow",
age: "31"
}

df = pd.read_json('values.json')

모든 스칼라 값을 사용하는 경우 인덱스를 전달해야 합니다.

판다는 그 값에서 목록이나 사전을 찾는다.고양이 같은 거.json

{
name: ["Snow"],
age: ["31"]
}

그러니 이렇게 해보세요.나중에 html tohtml()로 변환합니다.

df = pd.DataFrame([pd.read_json(report_file,  typ='series')])
result = df.to_html()

이렇게 배열로 변환하여 해결했습니다.

[{"biennials": 522004, "lb915": 116290, "shatzky": 127647, "woode": 174106, "damfunk": 133206, "nualart": 153444, "hatefillot": 164111, "missionborn": 261765, "yeardescribed": 161075, "theoryhe": 521685}]

언급URL : https://stackoverflow.com/questions/38380795/pandas-read-json-if-using-all-scalar-values-you-must-pass-an-index

반응형