python - 匹配两个打印中的相同单词

标签 python csv pandas

我正在使用 os 列出目录中的文件名。我还使用 pandas 列出 CSV 文件中一列的内容。我已经打印了两份打印品的结果,现在我想匹配两份打印品中出现的名称,并确定哪些名称是一份打印品所独有的。下面是我的代码,它获取 CSV 文件的名称和内容。

import os, sys
import pandas as pd


path = "/mydir/csvfile"
dirs = os.listdir( path )

for file in dirs:
    print file

fields = ['Column']

df = pd.read_csv('/mydir/csv_file', skipinitialspace=True, usecols=fields)

print df.Column

* 编辑*

我想出了这个可行的解决方案。

import os, sys
import pandas as pd


path = "/mdir/csvfile"
dirs = os.listdir( path )

list_1 = [file for file in dirs]



fields = ['column']

df =     pd.read_csv('/mydir/csvfile', skipinitialspace=True, usecols=fields)

list_2 = df.column.values.tolist()

list_3=[]


for i in list_1:
    if i in list_2:
        list_3.append(i + " True") 
    else:
        list_3.append(i + " False") 

print list_3

最佳答案

而不是

for file in dirs:
    print file

建立一个列表:

files = [file for file in dirs]

然后使用DataFrame来检查:

df.Column.isin(files)  # this will check elementwise
Out: 
0    True
1    True
2    True
3    True
Name: Column, dtype: bool

或者

df.Column.isin(files).all()  # if all of them are the same
Out: True

关于python - 匹配两个打印中的相同单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38745163/

相关文章:

python - Pandas 类别比较

python - 如何在 python 列表中查找彼此相邻的重复项并根据它们的索引列出它们?

javascript - 如何在 Angular JS 中加载 CSV 文件来制作图表

python - 导入 Python 异常

python - 在 scikit-learn 中运行 Randomforest 的 MemoryError

python - 如何在 Python 中将 csv 文件列中的最小/最大值查找为类似 JSON 的格式?

python - 如何为多个数据框项目运行代码

Python数据框,删除特定记录之后的所有内容

python - Pandas .DataFrame : find the index of the row whose value in a given column is closest to (but below) a specified value

python - 仅显示 Django 中的最新 3 个帖子?