I have a JSON file coming in, which I am doing some operations/trimming on.
The result looks like this:
JavaScript
x
3
1
print("User:", user)
2
> User: {'id': 1, 'label': 'female', 'position': {'lat': 47.72485566, 'lon': 10.32219439}, 'confidence': 0.8}
3
When applying df = pd.DataFrame(user, index=[0])
I get the following Dataframe:
JavaScript
1
3
1
id label position velocity
2
0 1 female NaN 0.8
3
When applying df = pd.DataFrame(user)
I get:
JavaScript
1
4
1
id label position confidence
2
lat 1 female 47.72485566 0.8
3
lon 1 female 10.32219439 0.8
4
I am aware, as to why that happens, however none is what I want.
I’d like the following:
JavaScript
1
3
1
id label lat lon confidence
2
0 1 female 47.72485566 10.32219439 0.8
3
However I am not sure what the best way is to split the position parameter.
Advertisement
Answer
You can just pandas.json_normalize
, then later rename the columns:
JavaScript
1
3
1
>>> df = pd.json_normalize({'id': 1, 'label': 'female', 'position': {'lat': 47.72485566, 'lon': 10.32219439}, 'confidence': 0.8})
2
>>> df = df.rename(columns={'position.lat': 'lattitude', 'position.lon': 'longitude'})
3
OUTPUT
JavaScript
1
3
1
id label confidence lattitude longitude
2
0 1 female 0.8 47.724856 10.322194
3