从数据框中删除控制字符空格 [英] remove control character whitespaces from dataframe

查看:93
本文介绍了从数据框中删除控制字符空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数据框df,通过它我可以使用此数据框获取列表的列表

I have a dataframe df by which I am getting list of list by using this

data = [list(map(str,n.tolist())) for n in df.values]

之后,我从这样的数据中替换特定的控制字符

after that I replace specific control character from data like this

data = [ [e.replace(u'\xa0', u'') for e in tempval ] for tempval in data ]

这很好用,但我希望在数据框本身中完成此操作,请提出一些建议.

This works fine but I want this to be done in dataframe itself please suggest something.

推荐答案

您可以使用

You can use DataFrame.replace:

df = pd.DataFrame({'A':['\xa0','s','w'],
                   'B':['s','w','v'],
                   'C':['e','d','\xa0']})

print (df)
   A  B  C
0     s  e
1  s  w  d
2  w  v  

然后用于创建listslist,通过DataFrame转换为numpy array .html"rel =" nofollow> values ,然后是tolist:

Then for creating list of lists convert DataFrame to numpy array by values and then tolist:

df.replace(u'\xa0',u'', regex=True, inplace=True)
#if need cast all values to str add astype
print (df.astype(str).values.tolist())
[['', 's', 'e'], ['s', 'w', 'd'], ['w', 'v', '']]

这篇关于从数据框中删除控制字符空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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