从Pandas数据框中检索特定部分字符串。

2024-10-03 09:20:51 发布

您现在位置:Python中文网/ 问答频道 /正文

我有个要求。我在一个领域有一些价值观:

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

。。。以此类推

这能做到吗


Tags: 数据nametreeconfcastle领域person热图
1条回答
网友
1楼 · 发布于 2024-10-03 09:20:51

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

完成后,选择要对空值执行的操作。一种选择是完全移除它们,如下所示。仅用置信水平隔离列是直接的

如果希望“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与dataframes/series无缝协作以创建热图。如果不知道你的最终目标,我不能在热图上给出建议,但是熊猫和海洋生物的文档是直截了当的

相关问题 更多 >