-1
I am trying to extract country names from a string type column that I have to create a new column in my Dataframe with just country names. The format is below:
["[{'iso_3166_1': 'US', 'name': 'United States of America'}]",
"[{'iso_3166_1': 'US', 'name': 'United States of America'}]",
"[{'iso_3166_1': 'US', 'name': 'United States of America'}]",
"[{'iso_3166_1': 'US', 'name': 'United States of America'}]",
"[{'iso_3166_1': 'US', 'name': 'United States of America'}]",
"[{'iso_3166_1': 'US', 'name': 'United States of America'}]",
"[{'iso_3166_1': 'DE', 'name': 'Germany'}, {'iso_3166_1': 'US', 'name': 'United States of America'}]",
"[{'iso_3166_1': 'US', 'name': 'United States of America'}]"]
It is important to note that some elements of the list contain two dictionaries, such as line 7th that has United States and Germany.
I thought of turning this column into a dictionary and then extracting the values from key name, but my loop fails when encountering problems like 7th line. For example:
Countries_Movie = []
for k in range(0,len(movies_datasets['production_countries'])):
if (type(movies_datasets.production_countries[k]) == str):
mv_inter = movies_datasets.production_countries[k].replace('[',"").replace(']',"")
mv_inter = ast.literal_eval(mv_inter)
mv_inter = mv_inter.get('name')
Countries_Movie.append(mv_inter)
Can you please suggest me a more efficient method or help me understand what is missing from my code?
Sincerely yours.