Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pandas - Find and Replace values starting with a '<'

I have values in a data frame with certain cells starting '< 101' and certain cells with '< 2' and some with '< 1'. I am trying to write regex using a Pandas Dataframe to find those cells and replace them entirely with the value '0.0'.

Here is what I have:

df_new2=df_new.replace('(?=<)','0', regex=True)

The above code simply adds a '0' in front of '< 101' with this output '0< 101'.

What I am looking for is an output that finds the '< 101' and replaces it with '0'. Is there a way to do this?

like image 884
Suresh Raja Avatar asked Sep 12 '25 18:09

Suresh Raja


1 Answers

df_new2 = df_new.replace('^.*<.*$', '0', regex=True)
like image 195
piRSquared Avatar answered Sep 15 '25 08:09

piRSquared