Janosh
Janosh

Reputation: 4732

Pandas drop column in place and return it

This seems clunky:

col = df['col']
df.drop(columns=['col'], inplace=True)

Is there a way to drop a columns and return it on the same line?

Upvotes: 5

Views: 2830

Answers (1)

Rakesh
Rakesh

Reputation: 82785

Looks like you need df.pop()

Ex:

df = pd.DataFrame(pd.np.random.randint(0, 10, (4,3)), columns=['a','b','c'])
a = df.pop("a") 

Upvotes: 11

Related Questions