亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 pandas 中從列表中提取數據作為字符串,并按值選擇數據?

如何在 pandas 中從列表中提取數據作為字符串,并按值選擇數據?

阿波羅的戰車 2023-09-05 20:24:00
我有一個像這樣的數據框:col1              col2[abc, bcd, dog]   [[.4], [.5], [.9]][cat, bcd, def]   [[.9], [.5], [.4]]列表中的數字col2描述了 中的元素(基于列表索引位置)col1。所以“.4”col2描述了“abc” col1。col1我想創建 2 個新列,其中一列僅提取中 >= .9 的元素col2,另一列作為col2;中的數字。所以兩行都是“.9”。結果:col3     col4[dog]   .9[cat]   .9我認為選擇從中刪除嵌套列表的路線col2就可以了。但這比聽起來更難。我已經嘗試了一個小時來移除那些指狀支架。嘗試:spec_chars3 = ["[","]"]for char in spec_chars3: # didn't work, turned everything to nan    df1['avg_jaro_company_word_scores'] = df1['avg_jaro_company_word_scores'].str.replace(char, '')df.col2.str.strip('[]') #didn't work b/c the nested list is still in a list, not a string我什至還沒弄清楚如何提取列表索引號并過濾 col1
查看完整描述

2 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

  • 根據問題末尾的解釋,似乎兩列都是str類型,并且需要轉換為list類型

    • .applymap與 一起使用ast.literal_eval

    • 如果只有一列是str類型,則使用df[col] = df[col].apply(literal_eval)

  • 每列中的數據列表必須使用以下方法提取pandas.DataFrame.explode

    • 外部explode將值從列表轉換為標量(即[0.4]轉換為0.4)。

  • 一旦值位于不同的行上,就可以使用布爾索引來選擇所需范圍內的數據。

  • 如果您想df與結合使用df_new,請使用df.join(df_new, rsuffix='_extracted')

  • 測試于python 3.10,pandas 1.4.3

import pandas as pd

from ast import literal_eval


# setup the test data: this data is lists

# data = {'c1': [['abc', 'bcd', 'dog'], ['cat', 'bcd', 'def']], 'c2': [[[.4], [.5], [.9]], [[.9], [.5], [.4]]]}


# setup the test data: this data is strings

data = {'c1': ["['abc', 'bcd', 'dog', 'cat']", "['cat', 'bcd', 'def']"], 'c2': ["[[.4], [.5], [.9], [1.0]]", "[[.9], [.5], [.4]]"]}


# create the dataframe

df = pd.DataFrame(data)


# the description leads me to think the data is columns of strings, not lists

# convert the columns from string type to list type

# the following line is only required if the columns are strings

df = df.applymap(literal_eval)


# explode the lists in each column, and the explode the remaining lists in 'c2'

df_new = df.explode(['c1', 'c2'], ignore_index=True).explode('c2')


# use Boolean Indexing to select the desired data

df_new = df_new[df_new['c2'] >= 0.9]


# display(df_new)

? ? c1? ?c2

2? dog? 0.9

3? cat? 1.0

4? cat? 0.9


查看完整回答
反對 回復 2023-09-05
?
慕村9548890

TA貢獻1884條經驗 獲得超4個贊

您可以使用列表推導式根據您的條件填充新列。


df['col3'] = [

    [value for value, score in zip(c1, c2) if score[0] >= 0.9]

    for c1, c2 in zip(df['col1'], df['col2'])

]

df['col4'] = [

    [score[0] for score in c2 if score[0] >= 0.9]

    for c2 in df['col2']

輸出


              col1                   col2   col3   col4

0  [abc, bcd, dog]  [[0.4], [0.5], [0.9]]  [dog]  [0.9]

1  [cat, bcd, def]  [[0.9], [0.5], [0.4]]  [cat]  [0.9]


查看完整回答
反對 回復 2023-09-05
  • 2 回答
  • 0 關注
  • 150 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號