How can I select Pandas.DataFrame by elements' length

前端 未结 2 1721

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(lam         


        
2条回答
  •  星月不相逢
    2021-01-24 17:40

    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
    

提交回复
热议问题