简体   繁体   English

从一列列表的列表中删除另一列列表中的元素并替换为新值 Python Pandas

[英]Remove elements from lists of a list in one column from a list in another column and replace with new values Python Pandas

I have a dataframe (column del_lst has bool type ):我有一个 dataframe(del_lst 列有 bool 类型):

import pandas as pd

df = pd.DataFrame({'col1': [[['a1']], [['b1'], ['b2']], [['b1'], ['b2']], [['c1'], ['c2'], ['c3']], [['c1'], ['c2'], ['c3']], [['c1'], ['c2'], ['c3']]],
'col2': [['a1'], ['b1'], ['b2'], ['c1'], ['c2'], ['c3']],
'day': [18, 19, 19, 20, 20, 20],
'del_lst': [True, True, True , True, False, False]})
df

Output: Output:

  col1                col2   day del_lst
0 [[a1]]                [a1]   18    True
1 [[b1], [b2]]        [b1]   19    True
2 [[b1], [b2]]        [b2]   19    True
3 [[c1], [c2], [c3]]  [c1]   20    True
4 [[c1], [c2], [c3]]  [c2]   20    False
5 [[c1], [c2], [c3]]  [c3]   20    False

I want to delete lists that have the True type, and delete them step by step.我想删除具有 True 类型的列表,并逐步删除它们。 For example in [[b1],[b2]] , b1 and b2 are True, so first delete b1 , then b2 .例如在[[b1],[b2]]中, b1b2为 True,因此首先删除b1 ,然后删除b2 I did like this, but unfortunately my code doesn't work.我确实喜欢这个,但不幸的是我的代码不起作用。

def func_del(df):
return list(set(df['col1']) - set(df['col2']))


def all_func(df):
# select only lines with True
df_tr = df[df['del_lst'] == True]
for i, row in df_tr.iterrows():
df_tr['new_col1'] = df_tr.apply(func_del, axis=1)

# I want to get a dictionary from where the key is column col1 and the value is new_col1
dict_replace = dict (zip(df_tr['col1'], df_tr['new_col1']))
# so that I replace the old values in the initial dataframe
df['col1_replaced'] = df['col1'].apply(lambda word: dict_replace.get(word, word))
return df

df_new = df.apply(all_func, axis=1)

I would like to have a dataframe like this at the end我想在最后有一个这样的 dataframe

   col1               col2  col1_replaced  day  del_lst
0 [[a1]]               [a1]   []             18     True
1 [[b1],[b2]]        [b1]   []             19     True
2 [[b1],[b2]]        [b2]   []             19     True
3 [[c1],[c2],[c3]]   [c1]   []             20     True
4 [[c1],[c2],[c3]]   [c2]   [[c2], [c3]]   20     False
5 [[c1],[c2],[c3]]   [c3]   [[c2], [c3]]   20     False

You need to loop here, using set operations:你需要在这里循环,使用set操作:

S = set(df.loc[df['del_lst'], 'col2'].str[0])


df['col1_replaced'] = [[x for x in l
                        if (x[0] if isinstance(x, list) else x) not in S]
                       for l in df['col1']]

NB I am assuming that you have either single or nested lists here, if not just use if x[0] not in S as condition注意我假设你在这里有单个列表或嵌套列表,如果不是,则只使用if x[0] not in S作为条件

output: output:

                 col1  col2  day  del_lst col1_replaced
0                [a1]  [a1]   18     True            []
1        [[b1], [b2]]  [b1]   19     True            []
2        [[b1], [b2]]  [b2]   19     True            []
3  [[c1], [c2], [c3]]  [c1]   20     True  [[c2], [c3]]
4  [[c1], [c2], [c3]]  [c2]   20    False  [[c2], [c3]]
5  [[c1], [c2], [c3]]  [c3]   20    False  [[c2], [c3]]

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如果熊猫数据框列中存在列表值列表,请用另一个熊猫列中的值替换它们 - If list of lists values are present in Pandas dataframe column replace them with values from another Pandas column Pandas:根据另一个列列表中的值对列列表进行排序 - Pandas: sort column lists based on values from another column list 从 pandas 列表列中,如何删除不在列表另一列中的所有值? - From a pandas column of lists, how to remove all values not in another column of list? 从列表的python列表和带有列表的列创建新的pandas数据框 - Create a new pandas dataframe from a python list of lists with a column with lists 从基于另一个列表的列表列中删除列表值 - Remove list values from a column of lists based on another list Python Pandas 将一列中的 NaN 替换为与列表列相同行的另一列中的值 - Python Pandas replace NaN in one column with value from another column of the same row it has be as list column 从 pandas 列中的列表中删除元素 - Remove elements from a list in a pandas column Python pandas 在另一列的元素列表中查找一列的元素 - Python pandas find element of one column in list of elements of another column Python Pandas:从List Column的值创建新行 - Python Pandas : Create new rows from values of a List Column Python Pandas 从另一列的列表中删除一列列表中的项目 - Python Pandas remove items from list in one column from the list in other column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM