1

I have a dictionary that looks like this:

dict = {
  "A": [1,2,3],
  "B": [4]
}

when I try to create a panda Dataframe I use:

output_df = pd.DataFrame.from_dict(dict, orient='index')

Output:

- 1 2 3
A 1 2 3
B 4

What I want:

- 1
A 1
A 2
A 3
B 4

Thanks for your help! :)

1 Answer 1

1

try:

df.stack().swaplevel(0,1)

1  A    1.0
2  A    2.0
3  A    3.0
1  B    4.0
dtype: float64

df.stack().swaplevel(0,1).reset_index(level=[1], name='a').reset_index(drop=True)

    level_1   a
0   A         1.0
1   A         2.0
2   A         3.0
3   B         4.0
Sign up to request clarification or add additional context in comments.

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.