Home > Enterprise >  How to use regular express in Pandas to get values that is outside of a parentheses
How to use regular express in Pandas to get values that is outside of a parentheses

Time:03-31

I have a df ,you can have it by run this code:

import numpy as np
import pandas as pd
from io import StringIO
df = """
  contract      
  888(G2M)
  432432
"""

df = pd.read_csv(StringIO(df.strip()), sep='\s ')
df

Output is:

    contract
0   888(G2M)
1   432432

My question is ,how can I remove the values inside of the parentheses ,the output should be:

    contract
0   888
1   432432

CodePudding user response:

You can try:

df['contract'] = df['contract'].str.replace('\(.*\)', '', regex=True)
print(df)

# Output
  contract
0      888
1   432432
  • Related