【问题标题】:How to extract specific key,value pairs from list of lists and add it to dataframe?如何从列表列表中提取特定的键、值对并将其添加到数据框中?
【发布时间】:2019-08-01 23:59:17
【问题描述】:

我正在使用 JSON 格式的数据框 (read_json)。我需要从数据框列之一的列表列表中提取特定值并将其附加为新列。

我的 DF:

df_with_name.info()
<class 'pandas.core.frame.DataFrame'>
Index: 286 entries, 500px to youtube.com
Data columns (total 7 columns):
documents    286 non-null object
logo         286 non-null object
name         286 non-null object
points       286 non-null object
rated        286 non-null object
see          2 non-null object
slug         286 non-null object
dtypes: object(7)
memory usage: 27.9+ KB

points 列包含列表列表,我可以这样访问:

df_with_name['points'][4]

从第 3 个位置的 df_with_name 打印列点。

输出:

[{'description': '“500px reserves the right, at its sole discretion, to modify or replace the terms at any time. If the alterations constitute a material change to the terms, 500px will notify you by Posting an announcement on the site. What constitutes a material change will be Determined at 500px’s sole discretion.”',
  'discussion': 'https://edit.tosdr.org/points/995',
  'id': '995',
  'point': 'bad',
  'score': 70,
  'title': ' Terms may be changed any time at their discretion, without notice to the user '},
 {'description': '“500px may terminate your store account at any time for any reason or no reason. All Store Images remaining in your Store account will be removed by 500px upon termination of your store account.”',
  'discussion': 'https://edit.tosdr.org/points/891',
  'id': '891',
  'point': 'bad',
  'score': 60,
  'title': '500px Store: your account can be terminated at any time'}]

我想完成什么:

如何访问数据帧中 286 个条目中每一个的 'title'、'score' 和 'point' 键值对,并将它们作为新条目附加到数据帧列(或从中创建新的数据框)?

我尝试了什么:

如果我将数据帧中的单数条目指定为字典,我可以访问有趣的键值对,如下所示:

df_as_dict['points'][1]['title']

然后我也可以使用.get() 循环,如下所示:

for v in df_as_dict.get('points'):
    print(v.get('title'))

但是当我尝试将所有内容放在一起或仅适用于数据帧的切片(单个条目,而不是整个 df)时,这会以某种方式失败。

我错过了什么?我的逻辑哪里错了?

【问题讨论】:

    标签: python pandas list dataframe


    【解决方案1】:

    假设l 是你的list

    pd.DataFrame(l)
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-10-31
      • 2023-02-24
      • 1970-01-01
      • 1970-01-01
      • 2021-11-11
      相关资源
      最近更新 更多