删除 pandas 数据框中的特殊字符 [英] Remove special characters in pandas dataframe

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

问题描述

这似乎是一项固有的简单任务,但是我发现很难从整个数据框中删除""并返回每一列中的数字值,包括没有'".该日期框架包括数百个其他列,简而言之如下:

This seems like an inherently simple task but I am finding it very difficult to remove the '' from my entire data frame and return the numeric values in each column, including the numbers that did not have ''. The dateframe includes hundreds of more columns and looks like this in short:

Time            A1      A2
2.0002546296    1499    1592
2.0006712963    1252    1459
2.0902546296    1731    2223
2.0906828704    1691    1904
2.1742245370    2364    3121
2.1764699074    2096    1942
2.7654050926    *7639*  *8196*
2.7658564815    *7088*  *7542*
2.9048958333    *8736*  *8459*
2.9053125000    *7778*  *7704*
2.9807175926    *6612*  *6593*
3.0585763889    *8520*  *9122*

我还没有写它来遍历df中的每一列,但是就第一列而言,我已经提出了

I have not written it to iterate over every column in df yet but as far as the first column goes I have come up with this

df['A1'].str.replace('*','').astype(float)

产生

0        NaN
1        NaN
2        NaN
3        NaN
4        NaN
5        NaN
6        NaN
7        NaN
8        NaN
9        NaN
10       NaN
11       NaN
12       NaN
13       NaN
14       NaN
15       NaN
16       NaN
17       NaN
18       NaN
19    7639.0
20    7088.0
21    8736.0
22    7778.0
23    6612.0
24    8520.0

是否有一种非常简单的方法来删除熊猫数据框中的"*"?

Is there a very easy way to just remove the '*' in the dataframe in pandas?

推荐答案

使用替换,适用于整个数据框:

use replace which applies on whole dataframe :

df
Out[14]: 
       Time      A1      A2
0  2.000255    1499    1592
1  2.176470    2096    1942
2  2.765405  *7639*  *8196*
3  2.765856  *7088*  *7542*
4  2.904896  *8736*  *8459*
5  2.905312  *7778*  *7704*
6  2.980718  *6612*  *6593*
7  3.058576  *8520*  *9122*

df=df.replace('\*','',regex=True).astype(float)

df
Out[16]: 
       Time    A1    A2
0  2.000255  1499  1592
1  2.176470  2096  1942
2  2.765405  7639  8196
3  2.765856  7088  7542
4  2.904896  8736  8459
5  2.905312  7778  7704
6  2.980718  6612  6593
7  3.058576  8520  9122

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

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