I have a dataframe:
title | cast
------------------------------
movie1 | cast1,cast2,cast3
movie2 | cast4,cast1,cast6,cast7
movie3 | cast4,cast3,cast5
pd.DataFrame({'movie': ['movie1','movie2','movie3'], 'cast': ['cast1,cast2,cast3','cast4,cast1,cast6,cast7','cast4,cast3,cast5']})
So, I want to get result something as :
cast | count
------------------------------
cast1 | 5
cast2 | 2
cast3 | 4
cast4 | 5
cast5 | 2
cast6 | 3
cast7 | 3
To do that,
df_cast = df.join(df.cast
.str.strip(',')
.str.split(',',expand=True)
.stack()
.reset_index(level=1,drop=True)
.rename('cast_member')).reset_index(drop=True)
This would add a new column cast_member with each cell having just one cast member name in it. I tried using groupby('cast_member') but, I am not sure how to proceed after that.
I am new to pandas so I would really appreciate an answer even though it could be a simple one.
