我正在尝试在数据框中创建一个新列,其中包含相应行的字数.我正在寻找单词的总数,而不是每个不同单词的频率.我认为会有一个简单/快速的方法来做到这一点共同的任务,但周围的Googling和阅读SO职位(一小撮后1,2,3,4)我卡住了.我已经尝试了在链接的SO帖子中提出的解决方案,但是回到了很多属性错误.
words = df['col'].split()
df['totalwords'] = len(words)
Run Code Online (Sandbox Code Playgroud)
结果是
AttributeError: 'Series' object has no attribute 'split'
Run Code Online (Sandbox Code Playgroud)
和
f = lambda x: len(x["col"].split()) -1
df['totalwords'] = df.apply(f, axis=1)
Run Code Online (Sandbox Code Playgroud)
结果是
AttributeError: ("'list' object has no attribute 'split'", 'occurred at index 0')
Run Code Online (Sandbox Code Playgroud) 我正在研究我的第一个Python项目,并拥有相当大的数据集(数十万行).我需要在5个文本列(每个'单元格的多个文本句子)上做一些nlp(聚类,分类),并且已经使用pandas来组织/构建数据集.我希望将spaCy用于所有nlp,但无法弄清楚如何对我的列中的文本进行标记.我已经阅读了一堆spaCy文档,然后用Google搜索,但我发现的所有例子都是单句或单词 - 而不是pandas df中的75K行.
我尝试过这样的事情:
df['new_col'] = [token for token in (df['col'])]
但肯定会感谢一些帮助/资源.
我遇到了这个错误
IOPub data rate exceeded.
The notebook server will temporarily stop sending output
to the client in order to avoid crashing it.
To change this limit, set the config variable
`--NotebookApp.iopub_data_rate_limit`.
Run Code Online (Sandbox Code Playgroud)
经过一分钟的谷歌搜索后发现解决方案似乎是开始与jupyter jupyter notebook --NotebookApp.iopub_data_rate_limit=10000000
但是,当我使用Anaconda Prompt执行此操作时,它会打开一个不同的jupyter窗口,其中包含不同的文件列表(它看起来像系统文件,扩展名为.exe的东西).这个我现有的Jupyter笔记本和Jupyter实验室没有注册增加的数据速率限制,如果我尝试打开任何文件,添加任何新文件或上传现有的jupyter笔记本,Anaconda Prompt都会返回权限错误.
我似乎无法找到任何关于此的文档 - 关于如何修复的任何想法?
我有一个包含~40列的数据集,我正在使用其中.apply(word_tokenize)
的5个,如下所示:df['token_column'] = df.column.apply(word_tokenize)
.
我只为其中一列获取了TypeError,我们将其称为problem_column
TypeError: expected string or bytes-like object
Run Code Online (Sandbox Code Playgroud)
这是完整的错误(剥离的df和列名,以及pii),我是Python的新手,我仍在试图找出错误消息的哪些部分是相关的:
TypeError Traceback (most recent call last)
<ipython-input-51-22429aec3622> in <module>()
----> 1 df['token_column'] = df.problem_column.apply(word_tokenize)
C:\Users\egagne\AppData\Local\Continuum\Anaconda3\lib\site-packages\pandas\core\series.py in apply(self, func, convert_dtype, args, **kwds)
2353 else:
2354 values = self.asobject
-> 2355 mapped = lib.map_infer(values, f, convert=convert_dtype)
2356
2357 if len(mapped) and isinstance(mapped[0], Series):
pandas\_libs\src\inference.pyx in pandas._libs.lib.map_infer (pandas\_libs\lib.c:66440)()
C:\Users\egagne\AppData\Local\Continuum\Anaconda3\lib\site-packages\nltk\tokenize\__init__.py in word_tokenize(text, language, preserve_line)
128 :type preserver_line: bool
129 """
--> 130 sentences = [text] if preserve_line else sent_tokenize(text, …
Run Code Online (Sandbox Code Playgroud) 我有一个带有几千行文本数据的df.我正在使用spaCy在该df的单个列上做一些NLP并且我正在尝试使用以下内容从我的文本数据中删除专有名词,停用单词和标点符号:
tokens = []
lemma = []
pos = []
for doc in nlp.pipe(df['TIP_all_txt'].astype('unicode').values, batch_size=9845,
n_threads=3):
if doc.is_parsed:
tokens.append([n.text for n in doc if not n.is_punct and not n.is_stop and not n.is_space and not n.is_propn])
lemma.append([n.lemma_ for n in doc if not n.is_punct and not n.is_stop and not n.is_space and not n.is_propn])
pos.append([n.pos_ for n in doc if not n.is_punct and not n.is_stop and not n.is_space and not n.is_propn])
else:
tokens.append(None)
lemma.append(None)
pos.append(None)
df['s_tokens_all_txt'] = tokens
df['s_lemmas_all_txt'] = lemma
df['s_pos_all_txt'] …
Run Code Online (Sandbox Code Playgroud) 目前我正在使用以下代码使用spaCy对一些文本数据进行lematize和计算TF-IDF值:
lemma = []
for doc in nlp.pipe(df['col'].astype('unicode').values, batch_size=9844,
n_threads=3):
if doc.is_parsed:
lemma.append([n.lemma_ for n in doc if not n.lemma_.is_punct | n.lemma_ != "-PRON-"])
else:
lemma.append(None)
df['lemma_col'] = lemma
vect = sklearn.feature_extraction.text.TfidfVectorizer()
lemmas = df['lemma_col'].apply(lambda x: ' '.join(x))
vect = sklearn.feature_extraction.text.TfidfVectorizer()
features = vect.fit_transform(lemmas)
feature_names = vect.get_feature_names()
dense = features.todense()
denselist = dense.tolist()
df = pd.DataFrame(denselist, columns=feature_names)
df = pd.DataFrame(denselist, columns=feature_names)
lemmas = pd.concat([lemmas, df])
df= pd.concat([df, lemmas])
Run Code Online (Sandbox Code Playgroud)
我需要删除专有名词,标点符号和停止单词,但在我当前的代码中执行此操作时遇到一些麻烦.我已经阅读了一些文档和其他资源,但现在遇到了错误:
---------------------------------------------------------------------------
AttributeError Traceback (most recent call last) …
Run Code Online (Sandbox Code Playgroud) 我有几个文件文件夹,我正在尝试将随机选择的文件样本从一个文件夹移动到另一个文件夹。我正在使用下面的代码,但它还没有完全运行。
import os, random, shutil
source='folder_path_1'
dest='dest_folder_path'
onlyfiles = [f for f in os.listdir(source) if os.path.isfile(os.path.join(source, f))]
no_of_files = round((len(onlyfiles)/5))
print(no_of_files)
for i in range(no_of_files):
random_file=random.choice(os.listdir(source))
source_file="%s\%s"%(source,random_file)
dest_file=dest
shutil.move(source_file,dest_file)
Run Code Online (Sandbox Code Playgroud)
这会产生多个错误。首先,dest
没有定义,如果我使用完整路径,文件不会移动(没有错误,只是没有移动)。