从 pandas 数据帧单元格中的凌乱字符串中删除换行符? [英] removing newlines from messy strings in pandas dataframe cells?

查看:29
本文介绍了从 pandas 数据帧单元格中的凌乱字符串中删除换行符?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了多种拆分和剥离 Pandas 数据框中的字符串的方法来删除所有 ' ' 字符,但出于某种原因,它根本不想删除附加到其他单词的字符,即使我把它们分开了.我有一个 Pandas 数据框,其中有一列使用 Beautifulsoup 从网页中捕获文本.Beautifulsoup 已经对文本进行了一些清理,但未能删除附加到其他字符的换行符.我的字符串看起来有点像这样:

I've used multiple ways of splitting and stripping the strings in my pandas dataframe to remove all the ' 'characters, but for some reason it simply doesn't want to delete the characters that are attached to other words, even though I split them. I have a pandas dataframe with a column that captures text from web pages using Beautifulsoup. The text has been cleaned a bit already by beautifulsoup, but it failed in removing the newlines attached to other characters. My strings look a bit like this:

动手 游戏开发.我们将学习各种与游戏相关的软件技术,包括编程语言、脚本 语言、操作系统、文件系统、网络、模拟 引擎和多媒体设计系统. 我们还将研究一些 来自计算机科学和相关 领域的潜在科学概念,包括

"hands-on development of games. We will study a variety of software technologies relevant to games including programming languages, scripting languages, operating systems, file systems, networks, simulation engines, and multi-media design systems. We will also study some of the underlying scientific concepts from computer science and related fields including"

是否有一种简单的python方法来删除这些 "字符?

Is there an easy python way to remove these " " characters?

提前致谢!

推荐答案

正确答案是:

df = df.replace(r'
',' ', regex=True) 

我认为你需要replace:

I think you need replace:

df = df.replace('
','', regex=True)

或者:

df = df.replace('
',' ', regex=True)

或者:

df = df.replace(r'\n',' ', regex=True)

示例:

text = '''hands-on
dev nologies
relevant scripting
lang
'''
df = pd.DataFrame({'A':[text]})
print (df)
                                                   A
0  hands-on
dev nologies
relevant scripting
la...

df = df.replace('
',' ', regex=True)
print (df)
                                                A
0  hands-on dev nologies relevant scripting lang 

这篇关于从 pandas 数据帧单元格中的凌乱字符串中删除换行符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆