0

I have a dataframe in which two columns are JSON objects. Something like this:

id     choice               name    host                                
002   {'option': 'true'}    Bob     {'city': {'name': 'A'}}
003   {'option': 'false'}   Ana     {'city': {'name': 'B'}}
004   {'option': 'false'}   Nic     {'city': {'name': 'C'}}

I wish for the column result to only be the final string in columns choice and host (true, false, A, B, C...)

i was able to do it to column host with the following formula

df['host'] = (df.loc[:, 'host']
                    .apply(lambda x: x['city']['name']))

This was succesful. However, when i apply something similar to column choice

df['choice'] = (df.loc[:, 'choice']
                         .apply(lambda x: x['option']))

i get TypeError: 'NoneType' object is not subscriptable

How could i get a choice column with "true" and "false"?

4

1 回答 1

1

让我们使用str.get

df.choice.str.get('option')
0     true
1    false
2    false
Name: choice, dtype: object

df.host.str.get('city').str.get('name')
0    A
1    B
2    C
Name: host, dtype: object

首先确保它们object在您的两列中,dict如果不是,请通过以下方式进行转换ast.literal_eval

import ast
df[['choice','host']]=df[['choice','host']].applymap(ast.literal_eval)
于 2019-12-12T17:13:22.050 回答