Home > Net >  Pandas - Specify Slice Additional Column Label in loc()
Pandas - Specify Slice Additional Column Label in loc()

Time:05-16

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

CodePudding user response:

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

CodePudding user response:

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

CodePudding user response:

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.

  • Related