簡體   English   中英

熊貓替換列表列的值

[英]pandas replace values of a list column

我有這樣的數據框

ID 回饋
T223 [好,壞,壞]
T334 [平均,好,好]
feedback_dict = {'Good':1, 'Average':2, 'Bad':3}

使用這本字典我必須更換反饋列

ID 回饋
T223 [1, 3, 3]
T334 [2,1,1]

我嘗試了兩種方法,但都沒有用,我們將不勝感激。

method1:    
df = df.assign(Feedback=[feedback_dict.get(i,i)  for i in list(df['Feedback'])])

method2:
df['Feedback'] = df['Feedback'].apply(lambda x : [feedback_dict.get(i,i)  for i in list(x)])

對我來說,第二個解決方案有效,但之前必須將字符串轉換為列表:

import ast

df['Feedback'] = df['Feedback'].apply(ast.literal_eval)
#df['Feedback'] = df['Feedback'].str.strip('[]').str.split(',')

使用嵌套字典的第一個解決方案:

df = df.assign(Feedback=[[feedback_dict.get(i,i) for i in x] for x in df['Feedback']])


df['Feedback'] = df['Feedback'].apply(lambda x : [feedback_dict.get(i,i)  for i in list(x)])
print (df)
     ID    Feedback
0  T223  [1, 3, 3]
1  T334  [2, 1, 1]

編輯:如果列表缺少值,則使用if-else語句 - 非列表值被替換為空列表:

print (df)
     ID             Feedback
0  T223       [Good,Bad,Bad]
1  T334  [Average,Good,Good]
2   NaN                  NaN


feedback_dict = {'Good':1, 'Average':2, 'Bad':3}
df = df.assign(Feedback=[[feedback_dict.get(i,i) for i in x] if isinstance(x, list) else [] 
                            for x in df['Feedback']])

print (df)
     ID   Feedback
0  T223  [1, 3, 3]
1  T334  [2, 1, 1]
2   NaN         []

如果你的用例和這個例子一樣簡單,我不會推薦這種方法。 但是,如果它使項目的其他部分更容易,那么這里還有另一種選擇。

  1. df.explode()你的專欄(假設它是一個列表而不是文本;否則先將它轉換為一個列表)
  2. 使用df.replace()執行替換
  3. 使用df.groupby()df.agg()再次將行分組

例如,它看起來像這樣(假設變量已像您的問題中那樣聲明):

df = df.explode('Feedback')
df['Feedback'] = df['Feedback'].replace(feedback_dict)
df = df.groupby('ID').agg(list)
l , L = [] , []  # two list for adding new values into them

for lst in df.Feeback: # call the lists in the Feeback column
    for i in last: #calling each element in each lists
        if i == 'Good': #if the element is Good then:
            l.append(feedback_dict['Good'])   #append the value 1 to the first created list
        if i == 'Average':  #if the element is Average then:
            l.append(feedback_dict['Average'])  #append the value 2 to the first created list
        if i == 'Bad':   #if the element is Bad then:
            l.append(feedback_dict['Bad']) #append the value 3 to the first created list
L.append(l[:3])  # we need to split half of the first list to add as a list to the second list and the other half as another list to the second list we created
L.append(l[3:])
df['Feeback'] = L  #at the end just put the values of the second created list as feedback column

您非常接近解決方案。

我所做的是:

data.replace({'Good': '1', 'Average': '2', 'Bad': '3'}, regex=True)

並獲得您正在尋找的結果:

在此處輸入圖像描述

暫無
暫無

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

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