【发布时间】:2021-08-03 11:22:11
【问题描述】:
我有一个单词列表
names = ['ASO', 'TSLA', 'GME']
我有一个数据框
dt ... text
0 2021-03-19 14:59:49+00:00 ... I only need ASO:"@ASO[]ASO^%$ASO to hit 20 eod to make up for a...
1 2021-03-19 14:59:51+00:00 ... Oh this isn’t good
2 2021-03-19 14:59:51+00:00 ... lads why is my account covered in more red ink..
如果列表中的单词与每一行匹配,我需要创建一个函数。 如果列表中有 3 个或更多类似的单词,如第一行,我只想保留这个单词的一个版本,记住标志。我不在乎是否会有迹象,但保留一个版本的单词很重要 我想要的输出
dt ... text
0 2021-03-19 14:59:49+00:00 ... I only need ASO to hit 20 eod to make up for a...
1 2021-03-19 14:59:51+00:00 ... Oh this isn’t good
2 2021-03-19 14:59:51+00:00 ... lads why is my account covered in more
这是我尝试过的
price = pd.read_csv('top_20_tickers.csv')
names = list(price.columns)
names.pop(0)
discussion = pd.read_csv('wsb_comments.csv', error_bad_lines=False, index_col=False, dtype='unicode')
discussion = discussion.drop_duplicates('text')
discussion = discussion[discussion['text'].notnull()]
def check_words(sentence, names):
words = sentence.split()
count = 0
for word in words:
if word in names:
count += 1
return count > 3
discussion['Contains_4+_words'] = discussion.apply(lambda r: check_words(r.text, names), axis=1)
discussion = discussion[discussion['Contains_4+_words'] == False]
但它会删除整行但我需要合并某些单词 谢谢
【问题讨论】:
-
到目前为止你有什么尝试?
-
您的文字令人困惑。要么在其中放置适当的标点符号,要么尝试将其作为项目列表。
-
请看评论
-
为什么会有两个数据框?第二个是关于什么的?
-
第二个是我想查看第 1 行的输出,然后是第 1 行的固定版本