Pandas形替换列表列的值

xzv2uavs  于 2022-12-25  发布在  其他
关注(0)|答案(4)|浏览(132)

我有一个这样的 Dataframe
| 识别号|反馈|
| - ------|- ------|
| T223| [好,坏,坏]|
| T334| [一般、良好、良好]|

feedback_dict = {'Good':1, 'Average':2, 'Bad':3}

使用这本词典,我必须替换反馈栏
| 识别号|反馈|
| - ------|- ------|
| T223| [一、三、三]|
| T334| [二、一、一]|
我试了两种方法,但都不起作用,任何帮助都将不胜感激。

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)])
a11xaf1n

a11xaf1n1#

你就快找到答案了。
我所做的是:

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

得到你想要的结果
enter image description here

m3eecexj

m3eecexj2#

对我来说,第二个解决方案工作,但必要的字符串转换为列表之前:

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         []
x9ybnkn6

x9ybnkn63#

如果你的用例和这个例子一样简单,我不会推荐这个方法,但是,这里有另一个选项,它可以让你项目的其他部分更容易。

  1. df.explode()您的列(假设它是一个列表而不是文本;否则先将其转换为列表)
    1.使用df.replace()执行替换
    1.使用df.groupby()df.agg()将行重新组合在一起
    对于这个例子,它看起来像这样(假设变量已经像你的问题中那样声明):
df = df.explode('Feedback')
df['Feedback'] = df['Feedback'].replace(feedback_dict)
df = df.groupby('ID').agg(list)
sbdsn5lh

sbdsn5lh4#

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

相关问题