How to solve TypeError: string indices must be integers?

0

I have the result of a union with dataframe merge.

actor_crew = pd.merge(actor, crew, on='directors', how='inner')
basic_actor_crew = pd.merge(Title_Data, actor_crew, on='tconst', how='inner')
basic_actor_crew_ratings = pd.merge(basic_actor_crew, ratings, on='tconst', how='inner')
basic_actor_crew_ratings.head()

tconst  primaryTitle    originalTitle   genres  primaryName     primaryProfession   averageRating   numVotes    primaryName_size    primaryProfession_size

tt4283088   Battle of the Bastards  Battle of the Bastards  Action,Adventure,Drama  Miguel Sapochnik    [director, art_department, producer]    9.879267    154016  16  3

And in this function is where I get the error.

def get_director(x):
for i in x:
    if i['job'] == 'Director':
        return i['name']
return np.nan

**basic_actor_crew_ratings['director'] = basic_actor_crew_ratings['primaryProfession'].apply(get_director)**
basic_actor_crew_ratings['primaryProfession'] = basic_actor_crew_ratings['primaryProfession'].apply(lambda x: x[:3] if len(x) >=3 else x)
basic_actor_crew_ratings['primaryProfession'] = basic_actor_crew_ratings['primaryProfession'].apply(lambda x: [str.lower(i.replace(" ", "")) for i in x])

And I miss this error

117 def get_director(x):
118     for i in x:
--> 119         if i['job'] == 'Director':
120             return i['name']
121     return np.nan

** When I mention the code, it works.

Any suggestions on where to go for the solution?

    
asked by Bruno Soto 14.06.2018 в 19:35
source

0 answers