My dataframe 'json' contains a column with dictionary values. I want to be able to strip the dictionary of the key 'display name', so i just end up with the values of the location.
How do I do this?
CodePudding user response:
Use this:
import ast
df['jobLocation'] = df['jobLocation'].dropna().astype('str').apply(ast.literal_eval).str['displayName']
CodePudding user response:
df1 = pd.DataFrame({'user': ["a", "b", "c", "d"],
'location': [{'displayName': 'Pitsburgh, PA, USA'}, {'displayName': 'EXAMPLE1, PA, USA'}, {'displayName': 'EXAMPLE2, PA, USA'},
{'displayName': 'Pitsburgh, PA, USA'}]})
df1['location']= df1['location'].apply(lambda x: list(x.values())[0].split(", ")[0])
print(df1)
output:
user location
0 a Pitsburgh
1 b EXAMPLE1
2 c EXAMPLE2
3 d Pitsburgh