简体   繁体   中英

How can I convert a dataframe into a nested dictionary?

I have a dataframe that looks like this:

    ORG                 SURVEY_DATE     NOS
Asset Management    2018-04-23          1.0
Asset Management    2018-05-08          1.0
Asset Management    2018-10-29          1.0
CIO                 2018-11-08          1.0
CIO                 2018-11-13          2.0

And I want to convert it to a dictionary that looks like this.

{
  "Asset Management": {
    "2019-03-30": 50,
    "2019-03-31": 40,
    "2019-04-01": 20,
    "2019-04-02": 30
  },
  "CIO": {
    "2019-03-30": 10,
    "2019-03-31": 20,
  }
}

Assumming your dataframe is in a variable called df :

>>> df.groupby('ORG').apply(lambda f: {key: value for key, value in zip(f.SURVEY_DATE, f.NOS)} ).to_dict()
{'Asset Management': {'2018-04-23': 1.0, '2018-05-08': 1.0, '2018-10-29': 1.0},
 'CIO': {'2018-11-08': 1.0, '2018-11-13': 2.0}}

OK I updated my answer. Whola! Now it works.

In [9]: df
Out[9]:
                ORG SURVEY_DATE  NOS
0  Asset Management  2018-04-23  1.0
1  Asset Management  2018-05-08  1.0
2  Asset Management  2018-10-29  1.0
3               CIO  2018-11-08  1.0
4               CIO  2018-11-13  2.0

In [10]: df.groupby('ORG').apply(lambda x: dict(zip(x['SURVEY_DATE'],x['NOS']))).to_dict()
Out[10]:
{'Asset Management': {'2018-04-23': '1.0',
  '2018-05-08': '1.0',
  '2018-10-29': '1.0'},
 'CIO': {'2018-11-08': '1.0', '2018-11-13': '2.0'}}

Explanation: if you have 2 or more iteratives, you can loop through them simultaneously using zip :

x = [1,2,3]
y = [4,5,6]
for i,j in zip(x, y):
    print(i, j) # (1,4), (2,5), (3,6)

And I'm creating a dictionary from a tuple . Also lambda is just a shorthand for any one liner function definition:

foo = lambda x: x+1
# equivalent
def foo(x):
  return x+1

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