meuhfunk
meuhfunk

Reputation: 75

How I can report the presence of a bad character on all the data of a pandas dataframe?

On my dataframe :

enter image description here

what is the easiest way to browse the datas of my dataframe which are numbers and detect characters of type: ', "and report them ? Can we avoid the series?

I want to browse my dataframe and detect the single quote and double quote present in the values ​​and if there are any I just want to display a logging.error with the presence of the character on a specific row.

Example 1 : "Double quote are detected on R3 and R1"

Example 2 : "Simple quote are detected on R2"

Upvotes: 0

Views: 98

Answers (2)

SeaBean
SeaBean

Reputation: 23217

You can try:

print('Single quote are detected on row(s): ', df.apply(lambda x: x.astype(str).str.contains("'")).any(axis=1)[lambda x: x].index.tolist())


print('Double quote are detected on row(s): ', df.apply(lambda x: x.astype(str).str.contains('"')).any(axis=1)[lambda x: x].index.tolist())

Output:

Single quote are detected on row(s):  ['R2']

Double quote are detected on row(s):  ['R1', 'R3']

Upvotes: 1

U13-Forward
U13-Forward

Reputation: 71580

Try assert:

x = ''.join(df.astype(str).agg(''.join))
assert not ((':' in x) or ('"' in x)), "Bad characters on a data"

Upvotes: 0

Related Questions