簡體   English   中英

在 Pandas 數據框中檢索字符串的特定部分

[英]Retrieving a Specific part of string in a Pandas Data frame

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

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

... 等等等等

這能做到嗎?

您的問題有點不清楚,但首先將您的數據輸入到數據框中。

完成此操作后,選擇要對空值執行的操作。 一種選擇是完全刪除它們,如下所示。 從那里直接使用置信水平隔離列。

如果您希望 '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 文檔是直截了當的。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM