Home > Net >  Finding cells that contain ' ' or '-' sign - Pandas
Finding cells that contain ' ' or '-' sign - Pandas

Time:12-07

Trying to see what cells have a ' ' sign in them, which cells have '-' and which have both.

df = pd.DataFrame({"result":['XY: (-Y, 25%)', 'XX: (-5q, 20%);( 18, 20%)', 'XX: (-6q25.3-q27, 11.8Mb, 30%)', 'XX: (-1, 25%);(-10q, 20%)', 'XX: ( 5, 20%)']})  
df

I tried using str.contain

gain=df.loc[df['result'].str.contains(" 7")]
gain

but got an error when trying to pass one of those characters ( or -) error: nothing to repeat at position 0. When passing just a number

gain=df.loc[df['result'].str.contains("7")]

it works great - no errors

Looking for some advice. Thanks

CodePudding user response:

Put backslash (escape):

gain=df.loc[df['result'].str.contains("\ 7")]

CodePudding user response:

Trying to see what cells have a ' ' sign in them, which cells have '-' and which have both.

you can try:

pos=[]
neg=[]
for index in df.index:
    if ' ' in df.loc[index,'result']:
        pos.extend([index])
    if '-' in df.loc[index,'result']:
        neg.extend([index])

and check if any pos is in neg like:

both = []
for item in pos:
    if item in neg:
        both.extend([item])
print(both)
  • Related