简体   繁体   中英

Drop rows in pandas if they contains “???”

Im trying to drop rows in pandas that contains "???", it works for every other value except for "???", I do not know whats the problem.

This is my code (I have tried both types):

df = df[~df["text"].str.contains("?????", na=False)]
df = df[~df["text"].str.contains("?????")]

error that I'm getting:

re.error: nothing to repeat at position 0

It works for every other value except for "????". I have googled it, and looked all over this website but I couldnt find any solutions.

The parameter expects a regular expression, hence the error re.error . You can either escape the? inside the expression like this:

df = df[~df["text"].str.contains("\?\?\?\?\?")]

Or set regex=False as Vorsprung sugested:

df = df[~df["text"].str.contains("?????",regex=False)]

let's convert this into running code:

import numpy as np
import pandas as pd

data = {'A': ['abc', 'cxx???xx', '???',], 'B': ['add', 'ddb', 'c', ]}
df = pd.DataFrame.from_dict(data)
df

output:

    A   B
0   abc add
1   cxx???xx    ddb
2   ??? c

with this:

df[df['A'].str.contains('???',regex=False)]

output:

    A   B
1   cxx???xx    ddb
2   ??? c

you need to tell contains() , that your search string is not a regex.

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