Pandas数据框过滤器不起作用,但str.match()起作用

问题描述 投票:0回答:1

我有一个熊猫数据框words_df,其中包含一些英语单词。

它只有一个名为word的列,其中包含英语单词。

words_df.tail()

enter image description here

words_df.dtypes

enter image description here

我想过滤掉包含单词[[zythum的行]

使用熊猫系列str.match()给了我预期的输出:

words_df[words_df.word.str.match('zythum')]

我知道str.match()不是正确的方法,它还会返回包含诸如

zythums

之类的其他单词的行。enter image description here

但是,对Pandas Dataframe使用以下操作将返回一个空的Dataframe

words_df[words_df['word'] == 'zythum']

enter image description here

我想知道为什么会这样吗?

编辑1:我还将附加我的数据源和用于导入它的代码。

数据源(我使用了

“ csv.zip中的单词列表”):

https://www.bragitoff.com/2016/03/english-dictionary-in-csv-format/

数据框导入代码:

import pandas as pd import glob as glob import os as os import csv path = r'data/words/' # use your path all_files = glob.glob(path + "*.csv") li = [] for filename in all_files: df = pd.read_csv(filename, index_col=None, header=None, names = ['word'], engine='python', quoting=csv.QUOTE_NONE) li.append(df) words_df = pd.concat(li, axis=0, ignore_index=True)

编辑2:

这里是我的代码块,带有更简单的导入代码,但面临相同的问题。 (使用上述链接中的

Zword.csv

文件)enter image description here
我有一个熊猫数据框words_df,其中包含一些英语单词。它只有一列名为word的单词,其中包含英语单词。 words_df.tail():words_df.dtypes:我要过滤掉...
python pandas dataframe
1个回答
0
投票
您需要将整个列转换为str类型:
© www.soinside.com 2019 - 2024. All rights reserved.