[英]Split values from a nested dictionary into pandas dataframe
I have a nested dictionary which returns multiple columns and rows as one value.我有一个嵌套字典,它返回多个列和行作为一个值。 It's being fetched from the unofficial Google Trends API and the following query returns a dictionary of pandas.DataFrames.
它是从非官方的 Google Trends API 获取的,以下查询返回一个 pandas.DataFrames 的字典。
# Related Queries, returns a dictionary of dataframes
related_queries_dict = pytrends.related_queries()
print(related_queries_dict)
Result:结果:
{'jeans': {'top': query value
0 mens jeans 100
1 skinny jeans 92
2 black jeans 84
3 womens jeans 62
4 blue jeans 58
5 white jeans 55
6 ripped jeans 54
7 best jeans 42
8 levis jeans 41
9 levis 41
10 denim jeans 38
11 american eagle 37
12 american eagle jeans 36
13 levi jeans 33
14 levi 33
15 mom jeans 30
16 jeans for men 28
17 jeans for women 28
18 hollister jeans 26
19 hollister 25
20 high waisted jeans 24
21 wrangler jeans 24
22 wrangler 23
23 plus size jeans 21
24 boyfriend jeans 20, 'rising': query value
0 extreme cut jeans 6450
1 extreme cut out jeans 5800
2 skinnygirl jeans 3000
3 mugsy jeans 200
4 cut out jeans 170
5 skinny girl jeans 160
6 everlane jeans 160
7 levi mom jeans 140
8 judy blue jeans 120
9 not your daughters jeans 120
10 kancan jeans 110
11 my fit jeans 100
12 levis wedgie jeans 100
13 amiri jeans 90
14 wrangler jeans mens 90
15 mike amiri jeans 80
16 mom jeans band 80
17 wit and wisdom jeans 70
18 bell bottom jeans 60
19 how to get blood out of jeans 60
20 just my size jeans 60
21 how to get grease out of jeans 50
22 ariat jeans 50
23 ymi jeans 50
24 mr. green jeans 50}}
I would like to split up the result into a pandas data frame so it looks something like this:我想将结果分成一个熊猫数据框,所以它看起来像这样:
+--------+----------------------+-------+
| Index | query | value |
+--------+----------------------+-------+
| 0 | mens jeans | 100 |
| 1 | skinny jeans | 92 |
| 2 | black jeans | 84 |
| 3 | womens jeans | 62 |
| 4 | blue jeans | 58 |
| 5 | white jeans | 55 |
| 6 | ripped jeans | 54 |
| 7 | best jeans | 42 |
| 8 | levis jeans | 41 |
| 9 | levis | 41 |
| 10 | denim jeans | 38 |
| 11 | american eagle | 37 |
| 12 | american eagle jeans | 36 |
| 13 | levi jeans | 33 |
| 14 | levi | 33 |
| 15 | mom jeans | 30 |
| 16 | jeans for men | 28 |
| 17 | jeans for women | 28 |
| 18 | hollister jeans | 26 |
| 19 | hollister | 25 |
| 20 | high waisted jeans | 24 |
| 21 | wrangler jeans | 24 |
| 22 | wrangler | 23 |
| 23 | plus size jeans | 21 |
+--------+----------------------+-------+
I have already searched for a similar answer on how to transform nested dictionaries into a pandas data frame but none of them takes into account splitting values.我已经搜索了关于如何将嵌套字典转换为 Pandas 数据框的类似答案,但它们都没有考虑拆分值。
I have no problem transforming it into a data frame using pd.DataFrame.from_dict which gives me the desired result although all values are in the same row:我使用 pd.DataFrame.from_dict 将其转换为数据框没有问题,尽管所有值都在同一行中,但它给了我所需的结果:
df_new = pd.DataFrame.from_dict(related_queries_dict, orient='index')
df_new.head()
Result:结果:
+-------+-------------------+-------------------+
| | top | rising |
+-------+-------------------+-------------------+
| jeans | query value 0 ... | query value 0 ... |
+-------+-------------------+-------------------+
看起来 'top' 和 'rising' 已经是数据帧了,请尝试打印 call to type 以确认
print(type(related_queries_dict['jeans']['top']))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.