Simple DataFrame with sets columns:
df = pd.DataFrame({'a': [{0,1}, {1,2}, {}], 'b': [{1,2},{2,3,4}, {3}]})
df
a b
0 {0, 1} {1, 2}
1 {1, 2} {2, 3, 4}
2 {} {3}
I want to transform multiple specific sets columns into lists columns. I'm using apply
and this doesn't work:
df[['a','b']].apply(lambda x: list(x))
a b
0 {0, 1} {1, 2}
1 {1, 2} {2, 3, 4}
2 {} {3}
It works for a single column / Series though:
df['a'].apply(lambda x: list(x))
0 [0, 1]
1 [1, 2]
2 []
Name: a, dtype: object
And a different function, on a different DataFrame not involving lists, of course works on multiple columns as expected:
df2 = pd.DataFrame({'a':[0,1,2], 'b':[3,4,5]})
df2[['a','b']].apply(lambda x: x + 1)
a b
0 1 4
1 2 5
2 3 6
So is there a one-liner for what I want to do without traversing through columns?