Zheng
Zheng

Reputation: 339

in Python Pandas, how to get one column, then skip a few columns, then get continuous columns

I imported a csv file to a dataframe which I named as df.

df = pd.read_csv('csvfile.csv')

df has 8 columns, which have names A, B, C, D, E, F, G, H. then I need to get column B and columns E:H, and all the rows of these columns. I used df1 = df.loc[:,"E":"H"] to get columns E to H. But couldn't figure out how to get column B as well, still using "E":"H" in the code. The reason I want to keep "E":"H" is because the column names of E, F, G, H are very long, I tried to avoid typing all four names. with thanks.

Upvotes: 3

Views: 5596

Answers (1)

Jeremy McGibbon
Jeremy McGibbon

Reputation: 3785

df.loc[:, list(df.columns[1]) + list(df.columns[4:])]

If you want to automatically find the columns, you can use

column_list = list(df.columns)
B_index = column_list.index('B')
E_index = column_list.index('E')
H_index = column_list.index('H')
df.loc[:, list(df.columns[B_index]) + list(df.columns[E_index:H_index])]

This is four lines, but the code is very readable.

Alternatively, if you know the exact names of the columns beforehand, it may be best to store those names in a list, and then use

column_names = [
    'B',
    'E',
    'F',
    'G',
    'H'
]
df.loc[:, column_names]

Upvotes: 3

Related Questions