栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

pandas:选择名称以X开头的所有列的最佳方法

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

pandas:选择名称以X开头的所有列的最佳方法

只需执行列表推导即可创建您的列:

In [28]:filter_col = [col for col in df if col.startswith('foo')]filter_colOut[28]:['foo.aa', 'foo.bars', 'foo.fighters', 'foo.fox', 'foo.manchu']In [29]:df[filter_col]Out[29]:   foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu0     1.0         0  0        2         NA1     2.1         0  1        4          02     NaN         0NaN        1          03     4.7         0  0        0          04     5.6         0  0        0          05     6.8         1  0        5          0

另一种方法是从列创建序列,并使用向量化str方法

startswith

In [33]:df[df.columns[pd.Series(df.columns).str.startswith('foo')]]Out[33]:   foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu0     1.0         0  0        2         NA1     2.1         0  1        4          02     NaN         0NaN        1          03     4.7         0  0        0          04     5.6         0  0        0          05     6.8         1  0        5          0

为了实现您想要的目标,您需要添加以下内容以过滤不符合您的

==1
条件的值:

In [36]:df[df[df.columns[pd.Series(df.columns).str.startswith('foo')]]==1]Out[36]:   bar.baz  foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu nas.foo0      NaN       1       NaNNaN      NaN        NaN     NaN1      NaN     NaN       NaN  1      NaN        NaN     NaN2      NaN     NaN       NaNNaN        1        NaN     NaN3      NaN     NaN       NaNNaN      NaN        NaN     NaN4      NaN     NaN       NaNNaN      NaN        NaN     NaN5      NaN     NaN         1NaN      NaN        NaN     NaN

编辑

看到您想要复杂的答案后,确定为:

In [72]:df.loc[df[df[df.columns[pd.Series(df.columns).str.startswith('foo')]] == 1].dropna(how='all', axis=0).index]Out[72]:   bar.baz  foo.aa  foo.bars  foo.fighters  foo.fox foo.manchu nas.foo0      5.0     1.0         0  0        2         NA      NA1      5.0     2.1         0  1        4          0       02      6.0     NaN         0NaN        1          0       15      6.8     6.8         1  0        5          0       0


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/617358.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号