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

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

问题描述

我有一个名为 timedata 的不同列名称的大熊猫DataFrame,其中一些包含单词Vibration,有些偏心。有可能创建一个包含单词Vibration的列的数据框?



我已尝试使用

  vib = [] 
为我在timedata:
如果'Vibration'在i:
vib = vib.append(i)

然后根据这些列的指标创建一个DataFrame。这实际上看起来不是最有效的方法,我确信必须有一些简单的列表理解。



编辑



数据框的形式:

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

对不起,我有一个缓慢的一天!感谢任何帮助

解决方案

这样就可以手动选择所有列中包含Vibration的列:

  df [[col for col in df.columns ifVibrationin col]] 
/ pre>

您也可以使用过滤器方法执行相同操作:

  df.filter(like =Vibration)

如果你想做一个更灵活的过滤器,你可以使用 regex 选项。例如。查看Vibration或Ecc是否在列名中:

  df.filter(regex ='Ecc | 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?

I have tried using

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

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.

EDIT

Dataframe of form:

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]]

You can also do the same with the filter method:

df.filter(like="Vibration")

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天全站免登陆