I'm trying to select multiple columns from a pandas DataFrame but am having trouble doing so. Suppose I have the following DataFrame:
import pandas as pd
import numpy as np
cols = ['test','one','two','three','four','five','six','seven','eight','nine','ten']
df = pd.DataFrame(np.random.rand(10,11).round(2),columns=cols)
I want to select columns test
, two
, four
, five
, six
, seven
, eight
I know that if I want to select individual columns,
df[['test','two']]
and if I want to select consecutive columns,
df.loc[:,'four':'eight']
work just fine but how to I combine the two concisely?
I realize that for this specific example, writing
df[['test', 'two', 'four', 'five', 'six', 'seven', 'eight']]
works too but I want to know if there is a way to make use of the fact that most of the columns are consecutive here to save some time writing them all.