0

df.columns code not working.

When I run this code, it seems that there is an error somewhere, but I can't find it. Thanks for the help.

  • 1
    you might need `np.r_` for slicing ranges something like `df.columns[np.r_[1:3,4:6]]` – anky Jun 14 '20 at 15:22
  • Does this work in pandas dataframe as well? – Sriswaroop Koundinya Jun 14 '20 at 15:26
  • it works for indexes yes, take a look at the dummy code in my previous comment and try on your dataframe, bdw please don't post images , people cannot copy the example to try it, instead [please create a minimal,complete and verifable example](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples) – anky Jun 14 '20 at 15:29

1 Answers1

1

You can use np.r_ to concatenate ranges of column indices and use them in .iloc, passing e.g.: as the row index. For example:

df.iloc[:, np.r_[0:3, 6:10, 10:14]]

produces:

   Player       Span  Mat   HS    Ave    BF     SR  100  50  0  4s
0  A Vala  2014-2019   27  104  27.37  1082  68.29    1   2  1  58
...

Note however that column numbers passed after each colon are not included in the output.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41