0

I have a dictionary of values:

d = {
 0: [1.61122, 1.61544, 1.60593, 1.60862, 1.61386],
 3: [1.61962, 1.61734, 1.6195],
 1: [1.5967, 1.59462, 1.59579],
 2: [1.59062, 1.59279],
    }

I want to create a table in pandas where the keys of the dictionary are the index, with each index having multiple rows containing the dictionaries values, like so:

enter image description here

What method or tool would allow me to make a table like this?

1 Answer 1

2

Use the Series constructor and explode:

df = pd.Series(d).explode().reset_index(name='price')

output:

    index    price
0       0  1.61122
1       0  1.61544
2       0  1.60593
3       0  1.60862
4       0  1.61386
5       3  1.61962
6       3  1.61734
7       3   1.6195
8       1   1.5967
9       1  1.59462
10      1  1.59579
11      2  1.59062
12      2  1.59279
Sign up to request clarification or add additional context in comments.

2 Comments

This semi solves what I want, I will mark it as correct but I'm not sure pandas has the same functionality that excel has.
@lostinpython I can't say, I don't use excel. If you want to keep the index "grouped" do not reset_index

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.