I have this dictionary:
d = {'a': (1,2,3), 'b': (4,5,6)}
I would like it to be formed as a dataframe where the key is shown as row along with its corresponding values, like the table below:
| Keys | Values |
|---|---|
| a | 1 |
| a | 2 |
| a | 3 |
| b | 4 |
| b | 5 |
Any ideas?
I have this dictionary:
d = {'a': (1,2,3), 'b': (4,5,6)}
I would like it to be formed as a dataframe where the key is shown as row along with its corresponding values, like the table below:
| Keys | Values |
|---|---|
| a | 1 |
| a | 2 |
| a | 3 |
| b | 4 |
| b | 5 |
Any ideas?
d = {'a': (1,2,3), 'b': (4,5,6)}
df = pd.DataFrame(d).unstack().droplevel(1).reset_index().rename({'index':'Keys', 0:'Values'}, axis=1)
Output:
>>> df
Keys Values
0 a 1
1 a 2
2 a 3
3 b 4
4 b 5
5 b 6
Here is my suggestion.
df = pd.DataFrame({'Keys': list(dict.keys()), 'Values': list(dict.values())})
df = df.explode(column='Values').reset_index(drop=True
The output result is something like this:
Keys Values
0 a 1
1 a 2
2 a 3
3 b 4
4 b 5
5 b 6