Generation

generate functionSat, 25 Feb 2023

Write code that finds all rows in the rate_group column of Pandas with "%" at the end and removes it with regex. Rows without "%" must be replaced with np.nan.

import numpy as np import pandas as pd df=pd.DataFrame(data={'rate_group':['A%','B%', 'C']}) df['rate_group'] = df['rate_group'].str.replace('%', '') df['rate_group']=df['rate_group'].replace(r'^\s*$', np.nan, regex=True) print(df['rate_group'])

Questions about programming?Chat with your personal AI assistant