1
$\begingroup$

Below is my Dataframe format consisting of 2 columns (one is index and other is the data to be cleaned)

 0  ans({'A,B,C,Bad_QoS,Sort'})
 1  ans({'A,B,D,QoS_Miss1,Sort'})
  1. I want to remove special characters
  2. create a data frame for all comma separated items.

I have managed to first remove ans from all rows using:

ds_[col2] = ds_.replace('ans', '', regex=True)

> 0 ({'A,B,C,Bad_QoS,Sort'})
> 1 ({'A,B,D,QoS_Miss1,Sort'})

Then I try to apply replace regex, see below:

ds_['col2'] = ds_['col2'].str.replace( r' \(\{\' | \'\}\) ', '', regex=True)
ds_['col2'] 

I get no errors, but no changes.

How could I clean these characters and also create a data frame like below:

col1 col2 col3 col4  col5  col6 

0    A    B    C    Bad_Qos  Sort
1    A    B    D    Qos_Miss1 Sort
$\endgroup$

1 Answer 1

0
$\begingroup$

You can first split on ', and then on , and then remove unnecessary columns:

first

df[['col_21','col_22','col_23']] = df['col2'].str.split("'",expand=True)

and

df[['col_2','col_3','col_4','col_5','col_6']] = df['col_22'].str.split(",",expand=True)

finally remove rest.

$\endgroup$
4
  • $\begingroup$ Thank you, it worked beautifully :) $\endgroup$
    – Tarzan
    Oct 5, 2021 at 11:38
  • $\begingroup$ Cant upvote as I am new to the forum. $\endgroup$
    – Tarzan
    Oct 5, 2021 at 11:40
  • $\begingroup$ @Tarzan you can probably accept as correct but don't worry about it. $\endgroup$
    – serali
    Oct 5, 2021 at 11:40
  • $\begingroup$ I am not able to accept the answer as well. Don't see that option. Someone please accept the answer for me. thanks. $\endgroup$
    – Tarzan
    Oct 5, 2021 at 11:41

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.