Is there a way to select all but one column in a pandas DataFrame object? I've seen ways to delete a column, but I don't want to do that.
Asked
Active
Viewed 1.1e+01k times
6 Answers
154
use drop
method:
df.drop(column_name, axis=1)

HYRY
- 94,853
- 25
- 187
- 187
-
1what does axis = 1 mean here? – uniquegino Mar 19 '17 at 20:20
-
12@uniquegino It instructs the function to search the column space when looking for the entity to drop. If `axis=0`, it will search the row space – Brian Mar 30 '17 at 23:48
-
@BrianVanover Thanks Brian! – uniquegino Apr 04 '17 at 18:33
-
1I've always used this to permanently drop a column. Never thought to use it for selection. `scott.mind == "blown"` – Him Jun 14 '18 at 14:30
-
3Do not forget to add `inplace=True` or to assign it to a variable. – Philippe Remy Nov 13 '18 at 09:10
-
@Scott Does it permanently remove a column ? Or if I print dp` again I would have it again ? – Revolucion for Monica Sep 11 '19 at 10:12
-
@RevolucionforMonica it doesn't modify the original `DataFrame` unless you specify `inplace=True` – CervEd Jun 23 '22 at 06:47
20
you can just select the columns you want without deleting or dropping:
collist = ['col1', 'col2', 'col3']
df1 = df[collist]
Just pass a list of the columns you desire
You can also retrieve the list of columns and then select from that list
collist = df.columns.tolist()
# you can now select from this list any arbritrary range
df1 = df[collist[0:1]]
# or remove a column
collist.remove('col2')
# now select
df1 = df[collist]
# df1 will now only have 'col1' and 'col3'

EdChum
- 376,765
- 198
- 813
- 562
-
5Note: the question is for "all but one" columns not for selecting one column. – Yu Shen Jan 16 '14 at 10:12
-
@YuShen my answer shows how to select an arbritrary list of columns, it is not showing how to select a single column. – EdChum Jan 16 '14 at 10:21
-
@EdChum people seem to have been precipitate in downvoting this one. – Eric Walker Aug 21 '14 at 21:01
-
@EricWalker it's only rep nothing major plus I know this is an answer that is correct, others are free to think different but it doesn't make me think I should delete or edit this answer. In fact dropping a column would delete the column if you assigned the result of drop to itself e.g. `df = df.drop(col, axis=1)`. – EdChum Aug 21 '14 at 21:08
2
You could use numpy to build a mask:
import numpy as np
columns = df.columns
mask = np.ones(columns.shape, dtype=bool)
i = 4 #The specified column that you don't want to show
mask[i] = 0
df[columns[mask]]

efajardo
- 797
- 4
- 9
2
Just as an option, you can select all columns but one (or many) using a list comprehension and df.loc method:
select = [x for x in df.columns if x != "column_you_don't_want"]
df.loc[:, select]
In case you want to leave out more than one column you can try this:
columns_dont_want = ["col1", "col2"]
select = [x for x in df.columns if x not in columns_dont_want]
df.loc[:, select]

Ivan Calderon
- 580
- 6
- 14
-
3Don't do that. Do this `df.loc[:, ~df.columns.isin(["col1", "col2"])]`. Even then, just use `drop` with the list `["col1", "col2"]`. – Little Bobby Tables Jan 04 '18 at 16:49