'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...



Solution 1:[1]

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

Solution 2:[2]

Using @keramat's data, you can use select_columns from pyjanitor to select columns (it offers some flexibility in selection options):

#pip install pyjanitor
import pandas as pd
import janitor

df = {'A': ['1', '1', '1', '2', '2', '3', '3', '3'],
 'B': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
 'C': ['a', 'a', 'a', 'a', 'a', 'a', 'a', 'a'],
 'D': [1, 2, 3, 1, -1, -1, -2, -3]}

df = pd.DataFrame(df)

df.select_columns(slice('A', 'C'), 'D')

   A  B  C  D
0  1  a  a  1
1  1  a  a  2
2  1  a  a  3
3  2  a  a  1
4  2  a  a -1
5  3  a  a -1
6  3  a  a -2
7  3  a  a -3

You can't use the : shortcut for slice, you have to explicitly use the slice function.

If you can, kindly provide an example that closely matches what you have in mind, and I'll see if select_columns can help, or use some native pandas options.

Solution 3:[3]

Use:

#Preparing sample data
string = """A B C D
1 a a 1
1 a a 2
1 a a 3
2 a a 1
2 a a -1
3 a a -1
3 a a -2
3 a a -3"""


import numpy as np
data = [x.split() for x in string.split('\n')]
import pandas as pd
df = pd.DataFrame(np.array(data[1:]), columns = data[0])

# making a function to map col name to col index
c = df.columns
def indexer(col):
    return c.to_list().index(col)


# converting and appending other columns

start = indexer('A')
end = indexer('C')
cols = c[start:end].to_list()
cols.append('D')


df.loc[:, cols]

Output:

    A   B   D
0   1   a   1
1   1   a   2
2   1   a   3
3   2   a   1
4   2   a   -1
5   3   a   -1
6   3   a   -2
7   3   a   -3

Which includes both slice (A:C) and an specified column(D).

Solution 4:[4]

After some thinking, I came up with my own solution: extract the single column and slice separately and concat them. I like it less than some answers posted here but anyway:

pd.concat([pd.DataFrame(games.loc[:,'Platform']),games.loc[:,'NA_Sales':'Other_Sales']],axis=1)

did the exact same job as

df.loc[:, ['Platform',*df.loc[:,'NA_Sales':'Other_Sales'].columns]]

Thanks everyone for your support, I appreciated it.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Shubham Sharma
Solution 2 sammywemmy
Solution 3
Solution 4 RubenGeert