MountainWild
MountainWild

Reputation: 23

How can I select Pandas.DataFrame by elements' length

How can I select Pandas.DataFrame by elements' length.

import pandas as pd;
import numpy as np;

df=pd.DataFrame(np.random.randn(4,4).astype(str))

df.apply(lambda x: len(x[1]))




0    19
1    19
2    18
3    20
dtype: int64

here, we see there are three kinds of lengths.

I've searching for something like this kind operation df[len(df)==19], is it possible?

Upvotes: 1

Views: 761

Answers (2)

DSM
DSM

Reputation: 353059

You could take advantage of the vectorized string operations available under .str, instead of using apply:

>>> df.applymap(len)
    0   1   2   3
0  19  18  18  21
1  20  18  19  18
2  18  19  20  18
3  19  19  18  18
>>> df[1].str.len()
0    18
1    18
2    19
3    19
Name: 1, dtype: int64
>>> df.loc[df[1].str.len() == 19]
                     0                    1                     2                   3
2   0.2630843312551179  -0.4811731811687397  -0.04493981407412525  -0.378866831599991
3  -0.5116348949042413  0.07649572869385729    0.8899251802216082  0.5802762385702874

Upvotes: 1

Brian Pendleton
Brian Pendleton

Reputation: 829

Here is a simple example to show you what is going on:

import pandas as pd
import numpy as np

df=pd.DataFrame(np.random.randn(4,4).astype(str))

lengths = df.apply(lambda x: len(x[0]))
mask = lengths < 15

print df
print lengths
print mask
print df[mask]

Results in:

                 0                1                 2                 3 
0   0.315649003654   -1.20005871043  -0.0973557747322  -0.0727740019505 
1  -0.270800223158   -2.96509489589    0.822922470677     1.56021584947 
2   -2.36245475786  0.0763821870378      1.0540009757   -0.452842084388 
3   -1.03486927366  -0.269946751202   0.0611709385483   0.0114964425747 

0    14 
1    14 
2    16 
3    16 
dtype: int64 

0     True 
1     True 
2    False 
3    False 
dtype: bool 

                 0               1                 2                 3 
0   0.315649003654  -1.20005871043  -0.0973557747322  -0.0727740019505 
1  -0.270800223158  -2.96509489589    0.822922470677     1.56021584947 

Upvotes: 1

Related Questions