1

Assume I have this dataframe:

df = pd.DataFrame({'a' : (1, 2, 3),
                   'b' : (1, 2, 3),
                   'c' : ("one", "two", "three"),
                   'd' : (4, 5, 6),
                   'e' : (4, 5, 6),
                   'f' : (7, 8, 9),
                   'g' : (7, 8, 9),
                   'h' : (7, 8, 9)})

I am trying to select the first, third, & fifth until the last columns. Desired output would be:

   a      c  e  f  g  h
0  1    one  4  7  7  8
1  2    two  5  8  7  8
2  3  three  6  9  9  9

How do I select multiple columns that are not in consecutive manner using integer? I have tried the following:

df.iloc[,[0, 3, 5:]]
df.loc[,[0, 3, 5:]]
df.iloc[,[0, 3, 5:len(df.columns)]]
df.loc[,[0, 3, 5:len(df.columns)]]
df.iloc[,[0 + 3 + 5:]]
df.loc[,[0 + 3 + 5:]]
df.iloc[,[0 + 3 + 5:len(df.columns)]]
df.loc[,[0 + 3 + 5:len(df.columns)]]

None worked

Please advise

Agnes Lee
  • 322
  • 1
  • 12

2 Answers2

1

Use np.r_ for join slicers, python counts from 0, so for third column need 2 and from 5th column need 4::

df = df.iloc[:, np.r_[0, 2, 4:len(df.columns)]]
print (df)
   a      c  e  f  g  h
0  1    one  4  7  7  7
1  2    two  5  8  8  8
2  3  three  6  9  9  9
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
  • Lifesaver, if you don't mind, do you also have a solution for numpy data converted through `df.values`? – Agnes Lee Apr 13 '21 at 10:16
  • 1
    @AgnesLee - Do you think `a = df.to_numpy()` and then `arr = a[:, np.r_[0, 2, 4:a.shape[1]]]` ? – jezrael Apr 13 '21 at 10:18
0

You can also do this by flatten a list of list with any methods you like.

from pandas.core.common import flatten

df1 = df.iloc[:, list(flatten([[0, 2], range(4, len(df.columns))]))]

df2 = df.iloc[:, np.concatenate([[0, 2], range(4, len(df.columns))])]
Ynjxsjmh
  • 28,441
  • 6
  • 34
  • 52