如何通过元素的长度选择Pandas.DataFrame [英] How can I select Pandas.DataFrame by elements' length

查看:154
本文介绍了如何通过元素的长度选择Pandas.DataFrame的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何根据元素的长度选择Pandas.DataFrame.

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.

我正在搜索类似这样的操作df[len(df)==19],这可能吗?

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

推荐答案

您可以利用

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

这篇关于如何通过元素的长度选择Pandas.DataFrame的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆