Reputation: 3751
How will i slice pandas dataframe with large number of columns, where I do not wish to select specific and non-sequentially positioned columns? One option is to drop the specific columns, but can i do something like:
df = pd.DataFrame(np.random.randint(0,100,(2,10)),columns=list('abcdefghij'))
df.iloc[:,~[1,4,9]]
Upvotes: 2
Views: 2836
Reputation: 210872
you can do it this way:
In [66]: cols2exclude = [1,4,9]
In [67]: df.iloc[:, df.columns.difference(df.columns[cols2exclude])]
Out[67]:
a c d f g h i
0 12 37 39 46 22 71 37
1 72 3 17 30 11 26 73
or:
In [68]: df.iloc[:, ~df.columns.isin(df.columns[cols2exclude])]
Out[68]:
a c d f g h i
0 68 49 90 9 48 36 26
1 6 72 98 49 44 10 36
Upvotes: 4