Skip to content
Advertisement

Pandas – Specify Slice + Additional Column Label in loc()

When using loc, it seems I can either specify a list with separate column labels or a slice.

However, can I combine a slice with an additional column label and -if so- how?

I tried

games.loc[games.Platform.isin(['X360']),['Platform','NA_Sales':'Other_Sales']]

but this throws a syntax error…

Advertisement

Answer

Solution with loc

df.loc[:, ['D', *df.loc[:, 'A':'C'].columns]]

   D  A  B  C
0  1  1  a  a
1  2  1  a  a
2  3  1  a  a
3  1  2  a  a
4 -1  2  a  a
5 -1  3  a  a
6 -2  3  a  a
7 -3  3  a  a
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement