1

I have a csv file, which looks as follows:

Country/Region  1/22/20     1/23/20     1/24/20
Afghanistan     100         200          300
Albania         400         500           0
Algeria          20          30           70    

(The table shows the amount of cases in certain date and country)

I want to convert it into a dictionary in the format shown bellow:

{
"Country1": {"time": [1/22/20, 1/23/20,...], "cases": [0, 0,...],
"Country2": {"time": [1/22/20, 1/23/20,...], "cases": [0, 0,...],
...
}

By using to_dict('records') looks very similar, but not the same.

[{'Country/Region': 'Afghanistan', '1/22/20': 0, '1/23/20': 0, '1/24/20': 0,...}]

I have been trying to use groupby('Country/Region') and it make no sense.

How would you do it?

2 Answers 2

1

Use List Comprehension:

In [2017]: d = [{c: {'time': d.columns.tolist(), 'cases': d.values.tolist()[0]}} for c, d in df.set_index(['Country/Region']).groupby('Country/Region')]

In [2018]: d
Out[2018]: 
[{'Afghanistan': {'time': ['1/22/20', '1/23/20', '1/24/20'],
   'cases': [100, 200, 300]}},
 {'Albania': {'time': ['1/22/20', '1/23/20', '1/24/20'],
   'cases': [400, 500, 0]}},
 {'Algeria': {'time': ['1/22/20', '1/23/20', '1/24/20'],
   'cases': [20, 30, 70]}}]
Sign up to request clarification or add additional context in comments.

Comments

0

apply() on row is enough

df.set_index('Country/Region').apply(lambda row: {row.name: {"time": row.index.tolist(), "cases": row.tolist()}}, axis=1).tolist()

'''
[{'Afghanistan': {'time': ['1/22/20', '1/23/20', '1/24/20'], 'cases': [100, 200, 300]}},
 {'Albania': {'time': ['1/22/20', '1/23/20', '1/24/20'], 'cases': [400, 500, 0]}},
 {'Algeria': {'time': ['1/22/20', '1/23/20', '1/24/20'], 'cases': [20, 30, 70]}}]
'''

Comments

Your Answer

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

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.