I have a list lets say:
x_lst = [1,2,3,4,5,6,7,8,9]
and a df like:
col_A, col_B
3 [4,3,2]
9 [1,2,3,4,5]
1 [5]
2 [3]
with data types:
col_A int
col_B object
dtype: object
now i am trying to create a new column col_C that writes the difference between x_lst and the col_B
expected res:
col_A, col_B col_C
3 [4,3,2] False
9 [1,2,3,4,5] [6,7,8,9]
1 [5] False
2 [3] [1,2,4,5,6,7,8,9]
for this i have tried something:
df['col_C'] = np.where(df.col_A != df.col_B.str.len(), x_lst - df.col_B, 'FALSE')
but this gave error:
ValueError: operands could not be broadcast together with shapes (9,) (7964,)
and if i use tolist():
df['col_C'] = np.where(df.col_A != df.col_B.str.len(), x_lst - df.col_B.tolist(), 'FALSE')
i get:
TypeError: unsupported operand type(s) for -: 'list' and 'list'
question from:
https://stackoverflow.com/questions/65952978/pandas-new-column-with-difference-from-series-object-list