1

I want to find the number of times that the string '\N' appearing in each column of the dataframe df.

I've tried this:

for col in df.columns: 
   print(df[col].value_counts()['\N'])

And the system returns the error like

unicode error unicode cannot decode in the position 0-1

Do you know how to solve it?

1
  • Can you give an example of the data you're doing this on? It's very hard to figure out the cause of an error when we can't see the data you're working with. Commented May 20, 2021 at 10:34

1 Answer 1

1

The backslash () character is used to escape characters that otherwise have a special meaning, such as newline, backslash itself, or the quote character (see python lexical analysis)

Assume this df:

    a   b
0  \N   1
1  \N   4
2   K  \N

Using your code will yield:

for col in df.columns:    
    print(df[col].value_counts()['\N'])

  File "<ipython-input-83-64eb7c05f66f>", line 2
    print(df[col].value_counts()['\N'])
                                ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 0-1: malformed \N character escape

If you add an extra backlash, you will get:

for col in df.columns:    
          print(f"{col} has",df[col].value_counts()['\\N']," \\N in it")

a has 2  \N in it
b has 1  \N in it

You can also see this clearly if you use df.to_dict():

>>> df.to_dict()
Out[901]: {'a': {0: '\\N', 1: '\\N', 2: 'K'}, 'b': {0: '1', 1: '4', 2: '\\N'}}
                      ^         ^                                         ^
Sign up to request clarification or add additional context in comments.

2 Comments

But it shows that KeyError '\\N'. My code is: for col in df.columns: print(df[col].value_count()['\\N'])
I think you should type it exactly as I have it. please change value_count() to value_counts(). If the error still persists, please include a sample of your sample data at hand in your question.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.