I have a dataframe and I want to choose the columns between two different column which I only have their name. For example, in the following df, I want to select the columns between column 'a1'
and 'a4'
. I know that I can df[['a1','a2', 'a3', 'a4']]
. However, my df is a very large df and I can not write it in this way.
import pandas as pd
df = pd.DataFrame()
df['a'] = [1, 2]
df['a1'] = [1, 2]
df['a2'] = [10, 12]
df['a3'] = [1, -2]
df['a4'] = [1, 12]
df['a5'] = [12, 20]
df['a6'] = [11, 3]
At the end I want this:
a1 a2 a3 a4
0 1 10 1 1
1 2 12 -2 12
Do you have any solution? Thanks
CodePudding user response:
Try to use loc
. You can read more under indexing and selecting data
df.loc[:, 'a1':'a4']
a1 a2 a3 a4
0 1 10 1 1
1 2 12 -2 12