python - Mapping methods across multiple columns in a Pandas DataFrame -


i have pandas dataframe values lists:

import pandas pd  df = pd.dataframe({'x':[[1, 5], [1, 2]], 'y':[[1, 2, 5], [1, 3, 5]]}) df          x          y 0   [1, 5]  [1, 2, 5] 1   [1, 2]  [1, 3, 5] 

i want check if lists in x subsets of lists in y. individual lists, can using set(x).issubset(set(y)). how across pandas data columns?

so far, thing i've come use individual lists workaround, convert result pandas. seems bit complicated task:

foo = [set(df['x'][i]).issubset(set(df['y'][i])) in range(len(df['x']))]  foo = pd.dataframe(foo) foo.columns = ['x_sub_y'] pd.merge(df, foo, how = 'inner', left_index = true, right_index = true)           x          y   x_sub_y 0   [1, 5]  [1, 2, 5]   true 1   [1, 2]  [1, 3, 5]   false 

is there easier way achieve this? possibly using .map or .apply?

use set , issubset:

df.assign(x_sub_y = df.apply(lambda x: set(x.x).issubset(set(x.y)), axis=1)) 

output:

        x          y  x_sub_y 0  [1, 5]  [1, 2, 5]     true 1  [1, 2]  [1, 3, 5]    false 

Comments

Popular posts from this blog

javascript - WinJS appendTextAsync producing scheduler errors -

minify - Minimizing css files -

Sockets with kotlin -