Suppose I have the following:
df = pd.DataFrame({'a':range(2), 'b':range(2), 'c':range(2), 'd':range(2)})
I'd like to "pop" two columns ('c' and 'd') off the dataframe, into a new dataframe, leaving 'a' and 'b' behind in the original df. The following does not work:
df2 = df.pop(['c', 'd'])
Here's my error:
TypeError: '['c', 'd']' is an invalid key
Does anyone know a quick, classy solution, besides doing the following?
df2 = df[['c', 'd']]
df3 = df[['a', 'b']]
I know the above code is not that tedious to type, but this is why DataFrame.pop was invented--to save us a step when popping one column off a database.
df.pop([['c', 'd']])
? – ChootsMagoots Mar 16 '18 at 21:13pop
a single column. – ALollz Mar 16 '18 at 21:14pd.DataFrame([df.pop(x) for x in ['c', 'd']]).T
but I don't know if that's easier that your not-classy solution. – pault Mar 16 '18 at 21:15