gpt4 book ai didi

python - 从 df.columns 中删除非英语单词

转载 作者:太空宇宙 更新时间:2023-11-03 21:10:00 25 4
gpt4 key购买 nike

我将多个数据集附加在一起,不幸的是,在数据收集中,一些数据收集者添加了英语问题的翻译。

df['What is your name'] 在其他数据集中报告为 df['What is your name Como te llamas']

理想情况下,我只想要 df['你叫什么名字']

对于姓名列和许多其他列(年龄、住房等)都是如此。

我使用 nltk 使用以下代码删除列名称中的所有非英语单词:

df_t.columns = " ".join(w for w in nltk.wordpunct_tokenize(df_t.columns) 
if w.lower() in words or not w.isalpha())

但是我收到以下错误错误:

---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-21-65a4c079ba1a> in <module>()
34 df_t['File Name'] = df_t['File Name'].str.strip().str[-30:]
35 df_t.columns = df_t.columns.str.replace(r'(^.*female.*$)', 'n_female_workers')
---> 36 df_t.columns = " ".join(w for w in nltk.wordpunct_tokenize(df_t.columns) if w.lower() in words or not w.isalpha())
37
38 list_month.append(df_t)

~\Anaconda3\lib\site-packages\nltk\tokenize\regexp.py in tokenize(self, text)
129 # If our regexp matches tokens, use re.findall:
130 else:
--> 131 return self._regexp.findall(text)
132
133 def span_tokenize(self, text):

TypeError: expected string or bytes-like object

如何解决?

最佳答案

我认为您需要按列名称循环将标量string传递给wordpunct_tokenize函数:

df_t = pd.DataFrame(columns=['What is your name Como te llamas'])

words = ['what','is','your','name']
df_t.columns = [" ".join(w for w in nltk.wordpunct_tokenize(x)
if w.lower() in words or not w.isalpha())
for x in df_t.columns]
print (df_t)
Empty DataFrame
Columns: [What is your name]
Index: []

关于python - 从 df.columns 中删除非英语单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55135556/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com