3

I would like to choose a dataframe by df[:,(1,5:)] i.e. all rows and column number 1 and columns 5 to the end.

I have tried:

df.loc[1:3,('col_1','col_5':)]

Any easy way to do it by column number or column name (both)?

2 Answers 2

3

I think you need numpy.r_ for concanecate indices and len(df.columns) for dynamically get position of last column:

df.iloc[1:3,np.r_[1, 5:len(df.columns)]]

Sample:

np.random.seed(23)
df = pd.DataFrame(np.random.randint(10, size=(5,11)), columns=list('ABCDEFGHIJK'))
print (df)
   A  B  C  D  E  F  G  H  I  J  K
0  3  6  8  9  6  8  7  9  3  6  1
1  2  5  5  0  5  0  9  9  3  1  7
2  4  1  1  4  6  7  3  6  9  2  3
3  0  8  6  6  0  5  6  0  2  0  3
4  0  6  2  2  5  7  9  4  7  7  3

df = df.iloc[1:3,np.r_[1, 5:len(df.columns)]]
print (df)
   B  F  G  H  I  J  K
1  5  0  9  9  3  1  7
2  1  7  3  6  9  2  3

If need index from 1 to end:

df = df.iloc[1:,np.r_[1, 5:len(df.columns)]]
print (df)
   B  F  G  H  I  J  K
1  5  0  9  9  3  1  7
2  1  7  3  6  9  2  3
3  8  5  6  0  2  0  3
4  6  7  9  4  7  7  3

And also is possible use np.r_ for index (select first index values and from 3 to end):

df = df.iloc[np.r_[1, 3:len(df.index)],np.r_[1, 5:len(df.columns)]]
print (df)
   B  F  G  H  I  J  K
1  5  0  9  9  3  1  7
3  8  5  6  0  2  0  3
4  6  7  9  4  7  7  3
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, it works for me with iloc, but not with loc.
I change it also ;)
2

we can use np.r_[] method, which:

translates slice objects to concatenation along the first axis.

In [86]: df
Out[86]:
      col_0     col_1     col_2     col_3     col_4     col_5     col_6     col_7     col_8     col_9
0  0.167483  0.104568  0.636430  0.706476  0.031586  0.936212  0.051971  0.541296  0.709061  0.870969
1  0.714087  0.801728  0.339450  0.814825  0.080115  0.894817  0.547592  0.817298  0.452318  0.643578
2  0.526403  0.731590  0.081630  0.060352  0.247103  0.159545  0.871784  0.219214  0.975865  0.336896
3  0.182118  0.789699  0.658708  0.498196  0.555364  0.719202  0.228455  0.996334  0.974793  0.650326
4  0.199542  0.680228  0.072198  0.030653  0.257683  0.462623  0.868273  0.727169  0.742707  0.425493
5  0.345935  0.371039  0.987650  0.040109  0.867031  0.578675  0.438615  0.725258  0.486669  0.873423
6  0.900702  0.421721  0.276828  0.592350  0.912363  0.210662  0.622967  0.631560  0.733113  0.131568
7  0.715825  0.909033  0.179683  0.237543  0.971395  0.180977  0.854385  0.492278  0.247231  0.870750
8  0.445305  0.514817  0.359233  0.592951  0.163524  0.391082  0.969412  0.258133  0.656737  0.325190
9  0.773473  0.130874  0.969821  0.453790  0.236050  0.073497  0.169758  0.519774  0.337003  0.828883

In [87]: df.iloc[:, np.r_[1, 5:df.shape[1]]]
Out[87]:
      col_1     col_5     col_6     col_7     col_8     col_9
0  0.104568  0.936212  0.051971  0.541296  0.709061  0.870969
1  0.801728  0.894817  0.547592  0.817298  0.452318  0.643578
2  0.731590  0.159545  0.871784  0.219214  0.975865  0.336896
3  0.789699  0.719202  0.228455  0.996334  0.974793  0.650326
4  0.680228  0.462623  0.868273  0.727169  0.742707  0.425493
5  0.371039  0.578675  0.438615  0.725258  0.486669  0.873423
6  0.421721  0.210662  0.622967  0.631560  0.733113  0.131568
7  0.909033  0.180977  0.854385  0.492278  0.247231  0.870750
8  0.514817  0.391082  0.969412  0.258133  0.656737  0.325190
9  0.130874  0.073497  0.169758  0.519774  0.337003  0.828883

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.