替换python pandas数据帧中的部分字符串 [英] Replacing part of string in python pandas dataframe

查看:4588
本文介绍了替换python pandas数据帧中的部分字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似的问题,发布在这里:

I have a similar problem to the one posted here:

Pandas DataFrame:从列中的字符串中删除不需要的部分

从DataFrame中的字符串中删除换行符。基本上,我访问了一个api使用python的json模块,这一切ok。创建DataFrame也令人惊讶。但是,当我想最终将结果输出到一个csv,我得到一个卡住,因为有一些换行在csv文件中创建假新行。

I need to remove newline characters from within a string in a DataFrame. Basically, I've accessed an api using python's json module and that's all ok. Creating the DataFrame works amazingly, too. However, when I want to finally output the end result into a csv, I get a bit stuck, because there are newlines that are creating false 'new rows' in the csv file.

所以基本上我想改变这个:

So basically I'm trying to turn this:

'...这是一个段落。

'...this is a paragraph.

这是另一段...'

一个段落。这是另一个段落...'

'...this is a paragraph. And this is another paragraph...'

我不在乎保留任何种类的'\\\
'或任何特殊符号的段落。

I don't care about preserving any kind of '\n' or any special symbols for the paragraph break. So it can be stripped right out.

我尝试了一些变化:

misc['product_desc'] = misc['product_desc'].strip('\n')

AttributeError: 'Series' object has no attribute 'strip'

这里是另一个

misc['product_desc'] = misc['product_desc'].str.strip('\n')

TypeError: wrapper() takes exactly 1 argument (2 given)

misc['product_desc'] = misc['product_desc'].map(lambda x: x.strip('\n'))
misc['product_desc'] = misc['product_desc'].map(lambda x: x.strip('\n\t'))

没有错误消息,但换行符也不会消失。同样的事情:

There is no error message, but the newline characters don't go away, either. Same thing with this:

misc = misc.replace('\n', '')

写入csv行是:

misc_id.to_csv('C:\Users\jlalonde\Desktop\misc_w_id.csv', sep=' ', na_rep='', index=False, encoding='utf-8')

Pandas版本为0.9.1

Version of Pandas is 0.9.1

谢谢! :)

推荐答案

strip 只删除开头的指定字符结束字符串。如果要删除全部 \\\
,则需要使用替换

strip only removes the specified characters at the beginning and end of the string. If you want to remove all \n, you need to use replace.

misc['product_desc'] = misc['product_desc'].str.replace('\n', '')

这篇关于替换python pandas数据帧中的部分字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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