2017-04-16 5 views
1

PandasDataframe 또는 JSON 배열로 전달하려는 데이터가있는 2 개의 행이 있습니다. 내가 df.to_json(orient='index')를 사용하여 시도JSON으로의 팬더 데이터 프레임 변환

[{ 
    "Date": "2017-02-03", 
    "Text": "Sample Text1" 
}, 
{ 
    "Date": "2015-02-04", 
    "Text": "Sample Text2" 
}] 

하지만 keys

{"0":{"Date":"2017-02-03","Text""Sample Text1"},"1":{"Date":"2017-02-04","Text""Sample Text2"}} 

답변

2

index 값을 사용하는 것으로 보인다 출력은 꽤 괜찮되지 않습니다 :

JSON 요구 사항은 다음과 포맷하기 사전 배열이 필요한 경우 orient='records' :

>>> import pandas as pd 
>>> df = pd.DataFrame({ 
...  'Date': ['2017-02-03', '2015-02-04'], 
...  'Text': ['Sample Text 1', 'Sample Text 2'] 
... }) 
>>> df.to_json(orient='records') 
'[{"Date":"2017-02-03","Text":"Sample Text 1"},{"Date":"2015-02-04","Text":"Sample Text 2"}]'