How to subset dataframe using pandas?

Viewed 140

I wanted to subset columns which may or may not be adjacent to each other. In this pilot case, there are only 39 columns, so writing out full column names are possible but tedious. What I'm trying to do is only subset column based on indices.

df_sub = df.iloc[:,[10,11,29:37]]

But I get this error:

File "<ipython-input-46-d9362578c302>", line 1
    df_sub = df.iloc[:,[10,11,29:37]]
                                ^
SyntaxError: invalid syntax

Isn't there a way to subset columns with column indices which may not be next to each other?

Searching on google to do something like this only showed me methods on subsetting based on column names, index that are next to each other, or entering each indices separately e.g., here. What if there's a mixture with 100s of columns?

1 Answers

You can try something like this:

g = lambda x,y: range(x,y)

[10,11,*g(29,37)]

#[10, 11, 29, 30, 31, 32, 33, 34, 35, 36]

Time Comparison:

%%timeit -n 1000000
np.r_[10,11,29:37]

12.4 µs ± 123 ns per loop (mean ± std. dev. of 7 runs, 1000000 loops each)

%%timeit -n 1000000
g = lambda x,y: range(x,y)
[10,11,*g(29,37)]

338 ns ± 3.94 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)

Related