【问题标题】:How to extract the value from list with dataframe? [duplicate]如何使用数据框从列表中提取值? [复制]
【发布时间】:2021-05-17 19:32:10
【问题描述】:
for i in range(1, len(data)):
    currentItem = data[i]
  
    df.loc[i] = [data[i]['details']]
df
# Till here its working as I can extract the data.

但我只需要从细节中提取数值

当我使用下面的代码时:

df = pd.DataFrame(data['details'])
number_list = df['number'].tolist() #list with numbers

显示错误:

TypeError                                 Traceback (most recent call last)
<ipython-input-126-81553db4d7eb> in <module>
      4 df
      5 
----> 6 df = pd.DataFrame(data['details'])
      7 number_list = df['number'].tolist() #list with numbers

TypeError: list indices must be integers or slices, not str

这就是我的数据的样子

【问题讨论】:

  • 这个answer 的副本解释了如何处理你的数据,这是一列字符串,在规范化之前必须转换为字典。请不要再次发布相同的问题。

标签: python pandas dataframe matplotlib jupyter-notebook


【解决方案1】:

您可以通过在数字系列上调用 apply 来实现。下面的代码将创建一个新列。

df['extracted_number'] = df.details.apply(lambda x: x['number'])

您也可以使用常规的 python 循环理解来做到这一点

number_list = [x['number'] for x in df.details]

【讨论】:

    猜你喜欢
    • 2020-08-21
    • 1970-01-01
    • 2020-11-08
    • 2013-07-28
    • 1970-01-01
    • 2022-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多