【问题标题】:Retrieving a Specific part of string in a Pandas Data frame在 Pandas 数据框中检索字符串的特定部分
【发布时间】:2019-07-14 10:14:23
【问题描述】:

我有一个要求。我在一个字段中有一些值:

0      [{'name': 'Skyscraper', 'conf': 0.726202309131...

1      [{'name': 'Tree', 'conf': 0.7405981421470642, ...

2      [{'name': 'Castle', 'conf': 0.8047274947166443...

3      [{'name': 'Building', 'conf': 0.94974970817565...

4      [{'name': 'Airplane', 'conf': 0.79357206821441...

5      [{'name': 'Tree', 'conf': 0.8992922306060791, ...

6      [{'name': 'Tree', 'conf': 0.943131983280182, '...

7      [{'name': 'Snowboard', 'conf': 0.8854210376739...

8                                                     []

9      [{'name': 'Sculpture', 'conf': 0.6212946772575...

10     [{'name': 'Tree', 'conf': 0.9138262867927552, ...

11     [{'name': 'Person', 'conf': 0.9718038439750672...

12     [{'name': 'Person', 'conf': 0.9445680975914, '...

13     [{'name': 'Tree', 'conf': 0.8676704168319702, ...

14     [{'name': 'Person', 'conf': 0.9166923761367798...

15     [{'name': 'Tree', 'conf': 0.9771925806999208, ...

16     [{'name': 'Snowboard', 'conf': 0.6349108815193...

17     [{'name': 'Person', 'conf': 0.9804859161376952...

如果您查看第 8 行,我可能也会输入空数据。

要求是提取置信度并从中构建热图

基本上我需要一列,其值类似于

0.726

0.740

0.804

0.949

...等等等等

这样可以吗?

【问题讨论】:

  • 您好,欢迎来到堆栈溢出。请开始here 了解如何使用 Stackoverflow 正确提问。此外,您还应该使用此link here 来更好地了解您作为有问题的人需要做什么才能获得与您相关的答案。虽然在 Stackoverflow 上提出正确的问题需要时间,但最终总能得到回报,因为与以当前格式提出问题相比,您更有可能得到答案。

标签: python pandas seaborn


【解决方案1】:

您的问题有点不清楚,但首先将您的数据输入到数据框中。

完成此操作后,选择您要对空值执行的操作。一种选择是完全删除它们,如下所示。从那里直接隔离只有置信水平的列。

如果您希望“conf”列返回所需的小数点后位数,请在该列上使用“apply”方法和 lambda 表达式。

import pandas as pd

# create dataframe, assume your list of dictionaries is in a variable 'x'
df = pd.DataFrame(x)
#drop all NaN columns if desired. Otherwise look at pandas documentation for NaN handling options
df = df.dropna()

#isolating the 'conf' column and limiting output to three decimal places
conf_column = df['conf'].apply(lambda x: round(x,3))

这将返回一个熊猫系列。 Seaborn 与数据框/系列无缝协作以创建热图。在不了解您的最终目标的情况下,我无法就热图提供建议,但 pandas 和 seaborn 文档是直截了当的。

【讨论】:

  • 谢谢,但我遇到了一个关键错误。 pandas_libs\index.pyx 在 pandas._libs.index.IndexEngine.get_loc() pandas_libs\index.pyx 在 pandas._libs.index.IndexEngine.get_loc() pandas_libs\hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item( ) pandas_libs\hashtable_class_helper.pxi 在 pandas._libs.hashtable.PyObjectHashTable.get_item() KeyError: 'conf'
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-30
  • 1970-01-01
  • 2022-12-19
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2018-11-16
相关资源
最近更新 更多