This will have to be a two step process (you cannot get around this, because as rightly mentioned, pop
works for a single column and returns a Series).
First, slice df
(step 1), and then drop those columns (step 2).
df2 = df[['c', 'd']].copy()
df = df.drop(['c', 'd'], axis=1)
And here’s the one-liner version using pd.concat
:
df2 = pd.concat([df.pop(x) for x in ['c', 'd']], axis=1)
This is still a two step process, but you’re doing it in one line.
df
a b
0 0 0
1 1 1
df2
c d
0 0 0
1 1 1
With that said, I think there’s value in allowing pop
to take a list-like of column headers appropriately returning a DataFrame of popped columns. This would make a good feature request for GitHub, assuming one has the time to write one up.