根据列名称创建DataFrame的子集 [英] Create a subset of a DataFrame depending on column name

查看:105
本文介绍了根据列名称创建DataFrame的子集的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个名为timedata的大熊猫DataFrame,具有不同的列名,其中一些包含单词Vibration,有些古怪.是否可以创建仅包含振动"一词的列的数据框?

I have a pandas DataFrame called timedata with different column names, some of which contain the word Vibration, some eccentricity. Is is possible to create a dataframe of just the columns containing the word Vibration?

我尝试使用

vib=[]
for i in timedata:
    if 'Vibration' in i:
        vib=vib.append(i)

然后根据这些列的索引创建一个DataFrame.确实,这似乎并不是最有效的方法,而且我敢肯定,列表理解肯定必须做些简单的事情.

to then create a DataFrame based on the indicies of these columns. This really does not seem like the most efficient way to do it and I'm sure there must be something simple to do with list comprehension.

编辑

以下形式的数据框:

df = DataFrame({'Ch 1:Load': randn(10), 'Ch 2:Vibration Brg 1T ': randn(10), 'Ch 3:Eccentricity Brg 1H ': randn(10), 'Ch 4:Vibration Brg 2T ': randn(10)})

抱歉,我今天过得很慢!谢谢你的帮助

Sorry I'm having a slow day! thanks for any help

推荐答案

类似这样的方法可以手动选择其中所有带有振动"字样的列:

Something like this to manually select all columns with the word "Vibration" in it:

df[[col for col in df.columns if "Vibration" in col]]

您还可以使用filter方法执行相同操作:

You can also do the same with the filter method:

df.filter(like="Vibration")

如果您想做一个更灵活的过滤器,可以使用regex选项.例如.查看振动"或"Ecc"是否在列名称中:

If you want to do a more flexible filter, you can use the regex option. E.g. to look if "Vibration" or "Ecc" is in the column name:

df.filter(regex='Ecc|Vibration')

这篇关于根据列名称创建DataFrame的子集的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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