根据 Pandas Python 中另一个数据帧的条件从一个数据帧中删除行

坦迈耆那教

我有两个 Pandas 数据框在 python 中包含数百万行。我想根据三个条件从第一个数据框中删除包含秒数据框中单词的行:

  1. 如果单词连续出现在句子的开头
  2. 如果单词连续出现在句尾
  3. 如果单词出现在连续句子的中间(确切的单词,而不是子集)

例子:

第一个数据框:

This is the first sentence
Second this is another sentence
This is the third sentence forth
This is fifth sentence
This is fifth_sentence 

第二个数据框:

Second
forth
fifth

预期输出:

This is the first sentence
This is fifth_sentence 

请注意,我在两个数据框中都有数百万条记录,如何以最有效的方式处理和导出?

我试过了,但需要很长时间

import pandas as pd
import re

bad_words_file_data = pd.read_csv("words.txt", sep = ",", header = None)
sentences_file_data = pd.read_csv("setences.txt", sep = ".", header = None)

bad_words_index = []
for i in sentences_file_data.index:
    print("Processing Sentence:- ", i, "\n")
    single_sentence = sentences_file_data[0][i]
    for j in bad_words_file_data.index:
        word = bad_words_file_data[0][j]
        if single_sentence.endswith(word) or single_sentence.startswith(word) or word in single_sentence.split(" "):
            bad_words_index.append(i)
            break
            
sentences_file_data = sentences_file_data.drop(index=bad_words_index)
sentences_file_data.to_csv("filtered.txt",header = None, index = False)

谢谢

索福克勒斯

您可以使用numpy.wherefunction 并创建一个名为“remove”的变量,如果您概述的条件得到满足,它将标记为 1。首先,创建一个包含值的列表df2

条件 1:将检查单元格值是否以列表中的任何值开头

条件 2:与上述相同,但它会检查单元格值是否以列表中的任何值结尾

条件 3:拆分每个单元格并检查拆分器字符串中是否有任何值在您的列表中

此后,您可以通过过滤掉以下内容来创建新的数据框1

# Imports
import pandas as pd
import numpy as np

# Get the values from df2 in a list
l = list(set(df2['col']))

# Set conditions
c = df['col']

cond = (c.str.startswith(tuple(l)) \
        |(c.str.endswith(tuple(l))) \
        |pd.DataFrame(c.str.split(' ').tolist()).isin(l).any(1))

# Assign 1 or 0
df['remove'] = np.where(cond,1,0)

# Create 
out = (df[df['remove']!=1]).drop(['remove'],axis=1)

out 印刷:

                          col
0  This is the first sentence
4      This is fifth_sentence

参考:

熊猫行选择字符串以列表中任何项目开头的位置

检查列是否包含列表中的任何 str

使用的数据帧:

>>> df.to_dict()

{'col': {0: 'This is the first sentence',
  1: 'Second this is another sentence',
  2: 'This is the third sentence forth',
  3: 'This is fifth sentence',
  4: 'This is fifth_sentence'}}

>>> df2.to_dict()

Out[80]: {'col': {0: 'Second', 1: 'forth', 2: 'fifth'}}

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

Python Pandas:如何根据条件库中的另一个数组替换数据帧中的值

如何根据另一个数据帧 Python3 的条件删除数据帧中的列

如何根据另一个数据帧中定义的行/列缩放因子缩放 Pandas 数据帧?

根据 Pandas 中的查找值从另一个数据帧中获取值

根据来自另一个数据帧的数据为 Pandas 数据帧中的列赋值

Pandas:如何根据信息的大小将信息从一个数据帧分配到另一个数据帧?

Python Pandas:将数据从一个数据帧转换为另一个数据帧

如何在另一个数据帧 python pandas 中的多列上使用条件逻辑在数据帧中创建一列?

Python Pandas根据在另一个数据框中的查找将列添加到数据框

Python:根据条件将信息从一个数据帧提取到另一个(具有不同长度)

Python Pandas:根据条件使用一个数据框的值填充另一个数据框的值

Python / Pandas:根据另一个数据框过滤和组织数据框的行和列

Pandas 根据另一个数据帧上的日期范围设置值

Python Pandas:根据另一个数据框的类别值创建新列

根据Python中的另一个数据框选择一个数据框的行

如何根据存储在另一个数据帧中的子字符串和行数过滤此 Pandas 数据帧?

根据来自另一个数据帧的条件更简单地删除熊猫数据帧中的行

使用 Pandas 根据来自另一个数据帧的行值填充列值

python - 如何将数据帧与python中pandas中另一个数据帧的子集交集进行比较?

Python Pandas:沿一列比较两个数据帧,并在另一个数据帧中返回两个数据帧的行内容

使用pandas python根据来自另一个数据框的数据更新数据框

Python Pandas合并两个数据帧,并将一个数据帧的一行映射到另一数据帧的所有行

根据R中的两个匹配条件,将值从一个数据帧添加到另一个数据帧

python pandas检查值在另一个数据帧的范围内

根据Pandas中的ID将列值从一个数据框复制到另一个数据框

通过部分匹配r中的另一个数据帧来对数据帧进行子集设置(对python / pandas解决方案开放)

根据另一个数据帧中的多个条件过滤数据帧

Python Pandas - 将带有“系列”的数据帧加入另一个数据帧

根据Pandas中的索引将一个数据帧分为多个