Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to filter a pandas dataframe based on the length of a entry

In a pandas dataframe I have a field 'amp' that should be populated by a list of length 495. Is there a panda-ic way to quickly filter on this length, such that all rows with field 'amp' are not equal to 495 are dropped?

I tried

df[len(df['amp']) == 495]

and this returned

KeyError: False
like image 478
Dan Steingart Avatar asked Oct 12 '16 18:10

Dan Steingart


People also ask

How do you filter the length of a string?

To filter strings of an Array based on length in JavaScript, call Array. filter() method on this String Array, and pass a function as argument that returns true for the specific condition on string length or false otherwise.

How do you use LEN in Pandas?

Get the number of rows: len(df) The number of rows of pandas. DataFrame can be obtained with the Python built-in function len() . In the example, it is displayed using print() , but len() returns an integer value, so it can be assigned to another variable or used for calculation.

How do you filter a DataFrame based on column values?

Using query() to Filter by Column Value in pandas DataFrame. query() function is used to filter rows based on column value in pandas. After applying the expression, it returns a new DataFrame.


2 Answers

If you specifically need len, then @MaxU's answer is best.

For a more general solution, you can use the map method of a Series.

df[df['amp'].map(len) == 495]

This will apply len to each element, which is what you want. With this method, you can use any arbitrary function, not just len.

like image 159
SethMMorton Avatar answered Oct 26 '22 12:10

SethMMorton


Try this:

df[df['amp'].str.len() == 495]

Demo:

In [77]: df
Out[77]:
                 a
0  [1, 2, 3, 4, 5]
1        [1, 2, 3]
2             [-1]

In [78]: df.a.str.len()
Out[78]:
0    5
1    3
2    1
Name: a, dtype: int64

In [79]: df[df.a.str.len() == 3]
Out[79]:
                 a
1        [1, 2, 3]
like image 25
MaxU - stop WAR against UA Avatar answered Oct 26 '22 12:10

MaxU - stop WAR against UA