1
$\begingroup$

I have a json array f below format

[{
    'Address': 'xxx',
    'Latitude': 28. xxx,
    'Longitude': 77. xxx,
    'reached': False
}, {
    'Address': 'yyy',
    'Latitude': 18. yyy,
    'Longitude': 73. yyy,
    'reached': False
}]

i want to convert into dataframe. if the column name is same it should have (Address_0, Address_1 etc) and should be side by side, not below. How can i do this?

$\endgroup$
1
  • $\begingroup$ Did you tried, what happens, what error shown? $\endgroup$ May 3, 2020 at 13:05

3 Answers 3

1
$\begingroup$

Have you tried using the pandas.read_json method? (documentation)

And it looks like your json is structured like 'records' so use

pd.read_json(_, orient='records')
$\endgroup$
5
  • 1
    $\begingroup$ im getting error ValueError: Invalid file path or buffer object type: <class 'list'> $\endgroup$
    – phoenix
    May 3, 2020 at 13:15
  • $\begingroup$ Did you change the underscore to the actual path? pd.read_json(PATH_HERE, orient='records') $\endgroup$
    – A Kareem
    May 3, 2020 at 13:29
  • $\begingroup$ The error you are getting is caused by the fact that the variable you are inputting (i.e. your data) is of type list, not a json string. $\endgroup$
    – Oxbowerce
    May 3, 2020 at 13:32
  • $\begingroup$ i have already read the input and parsed it. the above records are inside transit.. so what I have done is eachData['transits']=transitArray.. so above records are in transitarray.. so what should I do next? $\endgroup$
    – phoenix
    May 3, 2020 at 13:35
  • $\begingroup$ Where are you getting your input data from and how are you parsing it? The issue is coming from the fact that your data is a list instead of a string. $\endgroup$
    – Oxbowerce
    May 3, 2020 at 13:38
0
$\begingroup$

You can use the pd.DataFrame.from_records() method like:

pd.DataFrame.from_records(data)
$\endgroup$
0
$\begingroup$

Please use pd.json_normalize(data). It normalizes json structure into flat table of data:

data = [{
'Address': 'xxx',
'Latitude': 28.000,
'Longitude': 77.000,
'reached': False}, {
'Address': 'yyy',
'Latitude': 18.000,
'Longitude': 73.000,
'reached': False}]
pd.json_normalize(data)

enter image description here

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.