按列表选择 pandas 数据框 [英] Selecting pandas dataframe column by list

查看:77
本文介绍了按列表选择 pandas 数据框的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的一个脚本中,我通过列名列表选择一个数据框的几列.以下代码有效:

in one of my scripts I'm selecting several columns of a dataframe, by a list of the column names. The following code works:

data = df[lst]

只要列表的所有元素都包含在数据框中,它就可以正常工作.如果不是这种情况,它将返回错误'....'不在索引中".

It works fine as long as all elements of the list are included in the dataframe. If that's not the case, it will return the error "'....' not in index".

即使不是数据框中包含列表的所有元素,是否仍可以选择该列表中包含该列名称的所有列?

Is there a possibility to still select all columns which column name is included in that list, even if not all elements of the list are included in the dataframe?

推荐答案

我认为您需要

I think you need Index.intersection:

df = pd.DataFrame({'A':[1,2,3],
                   'B':[4,5,6],
                   'C':[7,8,9],
                   'D':[1,3,5],
                   'E':[5,3,6],
                   'F':[7,4,3]})

print (df)
   A  B  C  D  E  F
0  1  4  7  1  5  7
1  2  5  8  3  3  4
2  3  6  9  5  6  3

lst = ['A','R','B']

print (df.columns.intersection(lst))
Index(['A', 'B'], dtype='object')

data = df[df.columns.intersection(lst)]
print (data)
   A  B
0  1  4
1  2  5
2  3  6

具有 numpy.intersect1d 的另一种解决方案:

Another solution with numpy.intersect1d:

data = df[np.intersect1d(df.columns, lst)]
print (data)
   A  B
0  1  4
1  2  5
2  3  6

这篇关于按列表选择 pandas 数据框的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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