![](/img/trans.png)
[英]Python lambda function fail to remove the substring in Series
[英]Using lambda to remove word occurences below certain times in a series of Python
我在数据帧df 中有大量的 41,000 系列行:
column1 column2 column2
content in not below like this amsterdam nan sport
massive create non-programming question nan religion
我想删除column1 中所有出现次数低于 5 次或等于的单词,因此df数据框将如下所示:
column1 column2 column2
content amsterdam nan sport
massive create non-programming question nan religion
任何人都可以帮助我吗?
我最初的尝试是这样的:
df['column1'] = df['column1'].apply(filter(lambda x : (x, df['column1'].count < 4)), set(df['column1']))
但我收到错误消息说:
TypeError: filter expected 2 arguments, got 1
最好为此使用一个函数,因为单个 lambda 会太复杂而且不是很干净。
正则表达式将元素转换为单词列表。 然后过滤器保留大于 5 的单词。
import re
def remove_five_or_less(line):
word_list = re.sub("[^\w]", " ", line["column1"]).split()
filtered_list = filter(lambda x: len(x) > 5, word_list)
return " ".join(filtered_list)
df["column1"] = df.apply(lambda x: remove_five_or_less(x), axis=1)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.