'Add a column in the dataset which contains the total number of cast in that particular movie or Tv-Show

I have just started my work on pandas. Currently I'm working on a dataset of NETFLIX.

In this dataset I want to add a new column which contains the total number of cast members in that particular movie or tv show. I can calculate the cast individually but I want to calculate all of them. Can someone help me to write this code ?

NETFLIX.CSV FILE

Here is what I'm trying to do:

def set_cast(val):
    for i in df['cast']:
        if val== "None":
            return 0
        else:
            return len(df.loc[i,'cast'].split(', '))
df['num_of_cast'] = df['cast'].apply(set_cast)

That's how I'm trying to add number of cast in a new column but it's not working...The dataset contains 8807 rows so adding each of it individually is not possible for me.

Need a solution for this. Thanks



Solution 1:[1]

You are almost there

When you apply a function to a pd.Series, it is applied to each individual element of the series

So try this:

def set_cast(val):
    if val is None:
        return 0
    if val == 'None':
        return 0
    return len(val.split(', '))
df['num_of_cast'] = df['Cast'].apply(set_cast)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 piterbarg