【问题标题】:Split values from a nested dictionary into pandas dataframe将嵌套字典中的值拆分为 pandas 数据框
【发布时间】:2018-12-27 07:25:28
【问题描述】:

我有一个嵌套字典,它返回多个列和行作为一个值。它是从非官方的 Google Trends API 中获取的,以下查询返回 pandas.DataFrames 的字典。

# Related Queries, returns a dictionary of dataframes
related_queries_dict = pytrends.related_queries()
print(related_queries_dict)

结果:

 {'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}}

我想将结果拆分为 pandas 数据框,使其看起来像这样:

+--------+----------------------+-------+
| 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 |
+--------+----------------------+-------+

我已经搜索过关于如何将嵌套字典转换为 pandas 数据框的类似答案,但它们都没有考虑拆分值。

使用 pd.DataFrame.from_dict 将其转换为数据框没有问题,尽管所有值都在同一行中,但它给了我想要的结果:

df_new = pd.DataFrame.from_dict(related_queries_dict, orient='index')
df_new.head()

结果:

+-------+-------------------+-------------------+
|       |        top        |      rising       |
+-------+-------------------+-------------------+
| jeans | query value 0 ... | query value 0 ... |
+-------+-------------------+-------------------+

【问题讨论】:

  • 你能修复字典的格式吗?
  • 这是打印结果时的样子,很遗憾。我正在从 API 获取数据。
  • 你能做 print(type(result['jeans']['top'])),你看到你提供的打印看起来不可能,它可能是一个多行字符串,但引号在哪里和换行符?这是笔记本中的一些有线格式吗?
  • 是的,我正在使用 jupyter 笔记本。我更新了我的问题,希望能让自己更清楚一点。
  • 看起来 'top' 和 'rising' 已经是数据框了,试试吧。 print (type (related_queries_dict['jeans']['top']))

标签: python pandas dictionary dataframe nested


【解决方案1】:

看起来 'top' 和 'rising' 已经是数据框了,尝试打印 call to type 来确认

print(type(related_queries_dict['jeans']['top'])) 

【讨论】:

    猜你喜欢
    • 2022-08-19
    • 2021-11-28
    • 2022-01-07
    • 1970-01-01
    • 2015-10-06
    • 2015-07-24
    • 1970-01-01
    • 2021-12-09
    • 1970-01-01
    相关资源
    最近更新 更多