Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python convert Dict with multiple keys to Dataframe

I have a Dict created from web data. I start with an empty Dict, like d = {}. I then loop through each web page and store each page of data in d[i] where i starts out as 0, i = 0. I then have a Dict with multiple keys/indexes (I'm not sure what the proper terminology is). I can enter d[0] and see the first page, d[1] to see the second page and so on. I am trying to union each of these keys/indexes so that I can convert this Dict to a Dataframe. The Dict looks like this:

{0:    Age (Days) Number of Items in Queue Number of Items in WIP  \
 3           4                        0                      2
 4           5                        0                      1
 5           6                        0                      2
 6           7                        0                      4
 7           9                        0                      1
 8          12                        0                      2
 9          14                        0                      4
 10         15                        0                      3
 11         17                        0                      1
 12         19                        0                      2
1:    Age (Days) Number of Items in Queue Number of Items in WIP  \
 3           0                        0                      1
 4           1                        0                      8
 5           2                        0                      3
 6           3                        0                      6
 7           4                        0                      1
 8           5                        0                      7
 9           9                        0                      9
 10         10                        0                      4
 11         11                        0                      3
 12         12                        0                      8
 13         13                        0                      2
 14         14                        0                      1
 15         15                        0                      5
 16         16                        0                      6
 17         17                        0                      1
 18         18                        0                      5
 19         19                        0                      7
 20         20                        0                      2}

I can't simply use pd.DataFrame(d). I tried to use pd.DataFrame.from_dict(list(d.values())) but that didn't give the output I was expecting. I get the same output with pd.DataFrame.from_dict(d, orient='index'). The unwanted output is just listing each key/index and looks like this:

In [102]: pd.DataFrame.from_dict(d, orient='index')
Out[102]:
                                                   0
0     Age (Days) Number of Items in Queue Number ...
1     Age (Days) Number of Items in Queue Number ...
2     Age (Days) Number of Items in Queue Number ...

I'm expecting the rows from d[1] to be added to d[0] and keep the same column names.

like image 582
d84_n1nj4 Avatar asked Mar 13 '26 01:03

d84_n1nj4


1 Answers

IIUC you can concatenate them this way:

df = pd.concat(d.values(), ignore_index=True)
like image 168
MaxU - stop WAR against UA Avatar answered Mar 14 '26 14:03

MaxU - stop WAR against UA



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!