用pandas读取excel数据,用find函数查找特定字符串的字串,返回字符串并加入list

用pandas读取excel数据,用find函数查找特定字符串的字串,返回字符串并加入list

A=['问题','作业','编程','写','学','图书馆','实验','写作','自学']
for i in df2['操作']:
  for a in A:
    if i.find(a)!=-1:
      print(i)

现在只是能查找出来了,但后面如何把字符串去重并放入一个列表就没有思路了
我想要达到的结果是读取得到指定的字符串后,再用df.loc[]函数读取有关数据

实现代码如下:


A=['问题','作业','编程','写','学','图书馆','实验','写作','自学']
res_list = []
for i in df2['操作']:
    for a in A:
        if i.find(a)!=-1:
            print(i)
            res_list.append(i)

# 去重
res_list = list(set(res_list))
print(res_list)

望采纳


直接通过pandas操作完成呗,帮你写完了,代码如下:

img

# 字串列表对字段进行过滤,再对过滤后的数据进行去重
def filter_column_not_contain_and_drop_duplicates(df, column, substrings):
    for substring in substrings:
        data = df[~df[column].str.contains(substring)][column]
    return data.tolist()

substrings = ['问题','作业','编程','写','学','图书馆','实验','写作','自学']
result_list = filter_column_not_contain_and_drop_duplicates(df, '操作', substrings)

思路这么清晰~完全可以自己写出来吖

真的是基础不牢固啊我,唉,就差一点搞出来了!!!