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

查找名称包含特定字符串的列

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

查找名称包含特定字符串的列

只需遍历

Dataframe.columns
,现在是一个示例,在该示例中,您将获得匹配的列名称列表:

import pandas as pddata = {'spike-2': [1,2,3], 'hey spke': [4,5,6], 'spiked-in': [7,8,9], 'no': [10,11,12]}df = pd.Dataframe(data)spike_cols = [col for col in df.columns if 'spike' in col]print(list(df.columns))print(spike_cols)

输出:

['hey spke', 'no', 'spike-2', 'spiked-in']['spike-2', 'spiked-in']

说明:

  1. df.columns
    返回列名列表
  2. [col for col in df.columns if 'spike' in col]``df.columns
    使用变量遍历列表
    col
    并将其添加到结果列表(如果
    col
    包含)
    'spike'
    。此语法是列表理解。

如果只希望结果数据集的列匹配,则可以执行以下操作:

df2 = df.filter(regex='spike')print(df2)

输出:

   spike-2  spiked-in0        1          71        2          82        3          9


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

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

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