How do i remove special characters from a dataset in python?

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*

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]

which yields

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?

asked Jul 9, 2016 at 3:16

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

answered Jul 9, 2016 at 3:17

shivsnshivsn

7,10024 silver badges33 bronze badges

3

I found this to be a simple approach - Use replace to retain only the digits [and dot and minus sign].
This would remove characters, alphabets or anything that is not defined in to_replace attribute.

So, the solution is:
df['A1'].replace[regex=True, inplace=True, to_replace=r'[^0-9.\-]', value=r'']
df['A1'] = df['A1'].astype[float64]

answered Mar 28, 2018 at 14:18

There is another solution which uses map and strip functions. You can see the below link: Pandas DataFrame: remove unwanted parts from strings in a column.

df = 
    Time     A1     A2
0   2.0     1258    *1364*
1   2.1     *1254*  2002
2   2.2     1520    3364
3   2.3     *300*   *10056*

cols = ['A1', 'A2']
for col in cols:
    df[col] = df[col].map[lambda x: str[x].lstrip['*'].rstrip['*']].astype[float]

df = 
    Time     A1     A2
0   2.0     1258    1364
1   2.1     1254    2002
2   2.2     1520    3364
3   2.3     300     10056

The parsing procedure only be applied on the desired columns.

answered Nov 6, 2016 at 10:12

aminamin

1,33313 silver badges24 bronze badges

I found the answer of CuriousCoder so brief and useful but there must be a ']' instead of ']' So it should be:

df['A1'].replace[regex=True, inplace=True, to_replace=r'[^0-9.\-]',
value=r''] df['A1'] = df['A1'].astype[float64]

PV8

5,2604 gold badges38 silver badges70 bronze badges

answered Jun 27, 2019 at 9:01

How do I remove special characters from a data in Python?

Remove Special Characters Including Strings Using Python isalnum. Python has a special string method, . isalnum[] , which returns True if the string is an alpha-numeric character, and returns False if it is not. We can use this, to loop over a string and append, to a new string, only alpha-numeric characters.

How do you remove numbers and special characters from a DataFrame in Python?

Cast the column to string type by . astype[str] for in case some elements are non-strings in the column. Replace non alpha and non blank to empty string by str. replace[] with regex.

How do I remove special characters from a string without removing space in Python?

How to remove special characters from string Python [4 Ways].
Method 1 – Using isalmun[] method..
Method 2 – Using replace[] method​.
Method 3 – Using filter[].
Method 4 – Using join + generator function..

Chủ Đề