Is it possible to calculate the number of common items in a list column, between a row and the previous row in the method chain?
My code below throws and error ‘TypeError: unhashable type: ‘list”
import pandas as pd
df = pd.DataFrame({
'x':[1,2,3,4],
'list_column': [
['apple', 'banana', 'cherry'],
['banana', 'cherry'],
['cherry', 'date', 'fig'],
['orange']
]
})
res = len(set(df.loc[1,'list_column']) & set(df.loc[0,'list_column']))
res
df=(df
.assign(
list_length=lambda x: x['list_column'].str.len(),
nr_common=lambda x: (set(x['list_column']) & set(x['list_column'].shift(1))).len()
)
)
df