chexxmex
chexxmex

Reputation: 143

How to assert all values in a Pandas dataframe's column have a desired length

I have Pandas dataframe, that has the length of "state" column. I am 100% sure, the length of all the values in the column is 2. When I try to write an assert statement, I am getting an assertion error. I am not sure if I am using the assert statement correctly. Please help.

assert df['state'].str.len().all() == 2

My understanding: assert that all the values in the state column have a length that is equal to 2. Please let me know if this is correct. Thank You.

Upvotes: 5

Views: 5809

Answers (1)

Quang Hoang
Quang Hoang

Reputation: 150735

You have the order a bit messed up.

In your code df['state'].str.len().all() checks if all the lengths are different from 0, which returns a single True/False or 1/0. And when you compare that to a single 2, it's always False and trigger the assert.

It should be:

assert (df['state'].str.len() == 2).all()

Upvotes: 6

Related Questions