[英]Extract values from column of dictionaries using pandas
I am trying to extract the name from the below dictionary: 我试图从下面的字典中提取名称:
df = df[[x.get('Name') for x in df['Contact']]]
Given below is how my Dataframe looks like: 下面是我的Dataframe的样子:
data = [{'emp_id': 101,
'name': {'Name': 'Kevin',
'attributes': {'type': 'Contact',
'url': '/services/data/v38.0/sobjects/Contact/00985300000bt4HEG4'}}},
{'emp_id': 102,
'name': {'Name': 'Scott',
'attributes': {'type': 'Contact',
'url': '/services/data/v38.0/sobjects/Contact/00985300000yr5UTR9'}}}]
df = pd.DataFrame(data)
df
emp_id name
0 101 {'Name': 'Kevin', 'attributes': {'type': 'Cont...
1 102 {'Name': 'Scott', 'attributes': {'type': 'Cont...
I get an error: 我收到一个错误:
AttributeError: 'NoneType' object has no attribute 'get'
If there are no NaNs, use json_normalize
. 如果没有NaN,请使用
json_normalize
。
pd.io.json.json_normalize(df.name.tolist())['Name']
0 Kevin
1 Scott
Name: Name, dtype: object
If there are NaNs, you will need to drop them first. 如果有NaN,您需要先删除它们。 However, it is easy to retain the indices.
但是,保留指数很容易。
df
emp_id name
0 101.0 {'Name': 'Kevin', 'attributes': {'type': 'Cont...
1 102.0 NaN
2 103.0 {'Name': 'Scott', 'attributes': {'type': 'Cont...
idx = df.index[df.name.notna()]
names = pd.io.json.json_normalize(df.name.dropna().tolist())['Name']
names.index = idx
names
0 Kevin
2 Scott
Name: Name, dtype: object
Use apply
, and use tolist
to make it a list: 使用
apply
,并使用tolist
使其成为一个列表:
print(df['name'].apply(lambda x: x.get('Name')).tolist())
Output: 输出:
['Kevin', 'Scott']
If don't need list, want Series
, use: 如果不需要列表,想要
Series
,请使用:
print(df['name'].apply(lambda x: x.get('Name')))
Output: 输出:
0 Kevin
1 Scott
Name: name, dtype: object
Update: 更新:
print(df['name'].apply(lambda x: x['attributes'].get('Name')).tolist())
请尝试以下行:
names = [name.get('Name') for name in df['name']]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.