如何通过正则表达式从数据框中选择列 [英] How to select columns from dataframe by regex

查看:116
本文介绍了如何通过正则表达式从数据框中选择列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在python熊猫中有一个数据框.数据框的结构如下:

I have a dataframe in python pandas. The structure of the dataframe is as the following:

   a    b    c    d1   d2   d3 
   10   14   12   44  45    78

我想选择以d开头的列.有没有一种简单的方法可以在python中实现这一点.

I would like to select the columns which begin with d. Is there a simple way to achieve this in python .

推荐答案

您可以使用

You can use DataFrame.filter this way:

import pandas as pd

df = pd.DataFrame(np.array([[2,4,4],[4,3,3],[5,9,1]]),columns=['d','t','didi'])
>>
   d  t  didi
0  2  4     4
1  4  3     3
2  5  9     1

df.filter(regex=("d.*"))

>>
   d  didi
0  2     4
1  4     3
2  5     1

想法是通过regex

这篇关于如何通过正则表达式从数据框中选择列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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