简体   繁体   中英

Pandas to_dict data structure, using column as dictionary index

This is just a very specific data structure transformation that I'm trying to achieve with pandas, so if you know how to do it, please share:)

Imagine I have a dataframe that looks like this

id value date
1 1 2021-04-01
1 5 2021-04-02
1 10 2021-04-03
2 3 2021-04-01
2 4 2021-04-02
2 11 2021-04-03

Now I want to transform this into an object, where the keys are the ids, and the values are arrays of information about that id. So it would look like this...

{
  '1': [
         { 'value': 1, 'date': '2021-04-01' },
         { 'value': 5, 'date': '2021-04-02' },
         { 'value': 10, 'date': '2021-04-03' }
       ],
  '2': [
         { 'value': 3, 'date': '2021-04-01' },
         { 'value': 4, 'date': '2021-04-02' },
         { 'value': 11, 'date': '2021-04-03' }
       ],
}

I imagine I have to use .to_dict() somehow, but I can't quite figure out how to do it?

Thoughts?

Edit: I've already figured out a brute-force way of doing it, I'm looking for something more elegant;)

You can use list comprehension after converting the dataframe to dict object.

But here's a more Pandas-way,if your id column is a real column of the dataframe,

df = df.set_index('id').T.to_dict()

If you meant id as the index of dataframe, just use,

df = df.T.to_dict()

You can use groupby() on id and then apply() to_dict() on each group:

df.groupby('id').apply(lambda x: x[['value', 'date']].to_dict(orient='records')).to_dict()
{1: [{'value': 1, 'date': '2021-04-01'}, {'value': 5, 'date': '2021-04-02'}, {'value': 10, 'date': '2021-04-03'}], 2: [{'value': 3, 'date': '2021-04-01'}, {'value': 4, 'date': '2021-04-02'}, {'value': 11, 'date': '2021-04-03'}]}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM