简体   繁体   中英

replace '()' from a dataframe pandas python

I have a dataframe and it contains some values like

the change (♠)
and the new (⦻)

my desired output is

the change
and the new

I have tried to use

df.columns = df.columns.str.strip(' ()')
df=df.replace('\()','',regex=False)

but nothing worked, can anyone help? Thanks

If you have dataframe:

              col1           col2
0           value1  the change ()
1    the change ()         value3
2           value2         value4
3  () other change            NaN

You can replace the () in whole dataframe:

df = df.apply(lambda x: x.str.replace(r"\s*\(\)\s*", "", regex=True))
print(df)

Prints:

           col1        col2
0        value1  the change
1    the change      value3
2        value2      value4
3  other change         NaN

EDIT: If you have df:

              col1            col2
0           value1  the change (♠)
1   the change (⦻)          value3
2           value2          value4
3  () other change             NaN

Then:

df = df.apply(lambda x: x.str.replace(r"\s*\(.*?\)\s*", "", regex=True))
print(df)

Prints:

           col1        col2
0        value1  the change
1    the change      value3
2        value2      value4
3  other change         NaN

You have almost done it. Just change regex = True in your code and modify the regex to remove the spaces as well.

Input dataset

         col1             col2
0   change ()             val1
1        val2  samplestring ()
2  change 2()          val 5()
df.replace(r'\s*\(\s*\)\s*', '', regex = True, inplace = True)

Output dataset:

       col1          col2
0    change          val1
1      val2  samplestring
2  change 2         val 5

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM