Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
176 views
in Technique[技术] by (71.8m points)

python - How to convert a nested dict to a dataframe, without converting the inner keys to columns

I've a nested dict that looks like this:

{'1995-01-03': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-04': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-05': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-06': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-09': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-10': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-11': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-12': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)},
 '1995-01-13': {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}}

but I do not want the nested dictionary to be converted to rows.

I would like it to be to like this:

        index                                            values
0  1995-01-03  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
1  1995-01-04  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
2  1995-01-05  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
3  1995-01-06  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
4  1995-01-09  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
5  1995-01-10  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
6  1995-01-11  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
7  1995-01-12  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
8  1995-01-13  {91: (0, 1), 74: (1, 2), 78: (1, 3), 85: (1, 4)}
question from:https://stackoverflow.com/questions/65877875/how-to-convert-a-nested-dict-to-a-dataframe-without-converting-the-inner-keys-t

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
  • Use pd.Series.
dfn = pd.Series(data, name='values').reset_index()

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...