在 pandas 中保留/切片特定列 [英] keep/slice specific columns in pandas

查看:69
本文介绍了在 pandas 中保留/切片特定列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我了解这些列切片方法:

I know about these column slice methods:

df2 = df[["col1", "col2", "col3"]]df2 = df.ix[:,0:2]

但是我想知道是否有一种方法可以在同一切片中从数据帧的前/中/末端对列进行切片,而无需具体列出每一个.

but I'm wondering if there is a way to slice columns from the front/middle/end of a dataframe in the same slice without specifically listing each one.

例如,数据列df具有以下列:col1,col2,col3,col4,col5和col6.

For example, a dataframe df with columns: col1, col2, col3, col4, col5 and col6.

有没有办法做这样的事情?

Is there a way to do something like this?

df2 = df.ix[:, [0:2, "col5"]]

我遇到的情况是我有数百个列,并且通常需要针对不同的请求对特定的列进行切片.我已经查看了文档,但没有看到类似的内容.我忽略了什么吗?

I'm in the situation where I have hundreds of columns and routinely need to slice specific ones for different requests. I've checked through the documentation and haven't seen something like this. Have I overlooked something?

推荐答案

IIUC,我能想到的最简单的方法就是这样:

IIUC, the simplest way I can think of would be something like this:

>>> import pandas as pd
>>> import numpy as np
>>> df = pd.DataFrame(np.random.randn(5, 10))
>>> df[list(df.columns[:2]) + [7]]
          0         1         7
0  0.210139  0.533249  1.780426
1  0.382136  0.083999 -0.392809
2 -0.237868  0.493646 -1.208330
3  1.242077 -0.781558  2.369851
4  1.910740 -0.643370  0.982876

其中list调用不是可选的,因为否则Index对象将尝试将其自身矢量添加到7中.

where the list call isn't optional because otherwise the Index object will try to vector-add itself to the 7.

可以对numpy的r_之类的东西进行特殊处理,以便

It would be possible to special-case something like numpy's r_ so that

df[col_[:2, "col5", 3:6]]

可以工作,尽管我不知道这是否值得麻烦.

would work, although I don't know if it would be worth the trouble.

这篇关于在 pandas 中保留/切片特定列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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