Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
408 views
in Technique[技术] by (71.8m points)

python - removing newlines from messy strings in pandas dataframe cells?

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"

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

Thanks in advance!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

EDIT: the correct answer to this is:

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

I think you need replace:

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

Or:

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

Or:

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

Sample:

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 

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...