'How to get multiple column-slices of a dataframe in pandas
for example,
from pandas import DataFrame
df = DataFrame(np.arange(8).reshape(1, 8), columns = list('abcdefgh'))
I want to select the columns 'b':'d' and 'f':'h', say both inclusive.
I know I could select 'b':'d' by executing:
df2 = df.loc[:, 'b':'d']
, but something like df2 = df.loc[:, ['b':'d', 'f':'h']]
which is like the syntax in MATLAB wouldn't work here, so how can I select several
columns in dataframe?
Solution 1:[1]
You can use numpy.r_
for concatenation of indices, but it works only with positions, so need get_loc
or searchsorted
+ iloc
:
df = pd.DataFrame(np.arange(8).reshape(1, 8), columns = list('abcdefgh'))
print (df)
a b c d e f g h
0 0 1 2 3 4 5 6 7
b = df.columns.get_loc('b')
d = df.columns.get_loc('d')
f = df.columns.get_loc('f')
h = df.columns.get_loc('h')
print (b,d,f,h)
1 3 5 7
b = df.columns.searchsorted('b')
d = df.columns.searchsorted('d')
f = df.columns.searchsorted('f')
h = df.columns.searchsorted('h')
print (b,d,f,h)
1 3 5 7
df = df.iloc[:, np.r_[b:c+1, f:h+1]]
print (df)
b c d f g h
0 1 2 3 5 6 7
It is same as:
df = df.iloc[:, np.r_[1:4, 5:8]]
print (df)
b c d f g h
0 1 2 3 5 6 7
df = df.iloc[:, np.r_['b':'d', 'f':'h']]
print (df)
#TypeError: unsupported operand type(s) for -: 'str' and 'str'
Another solution with loc
+ join
:
df = df.loc[:,'b':'d'].join(df.loc[:,'f':'h'])
print (df)
b c d f g h
0 1 2 3 5 6 7
Solution 2:[2]
Another way to do this using pd.concat:
pd.concat([df.loc[:,'b':'d'],df.loc[:,'f':'h']],1)
Out[608]:
b c d f g h
0 1 2 3 5 6 7
Solution 3:[3]
One option for selecting multiple slices is with select_columns from pyjanitor:
# pip install pyjanitor
import pandas as pd
import janitor
df.select_columns(slice('b', 'd'), slice('f', 'h'))
b c d f g h
0 1 2 3 5 6 7
The caveat here is that you have to explicitly use python's builtin slice
.
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 | |
Solution 2 | Allen Qin |
Solution 3 | sammywemmy |