String python熊猫数据框列的字符串值看起来像字典列表吗?
我有一个dataframe列,其中包含字符串值(已编辑) 数据帧列的类型为字符串,但(已编辑) 其值类似于字典列表(已编辑) 如何从该字符串中提取一些键值? 此字符串值类似于字典列表 当键“job”的值为“Director”时,如何提取String python熊猫数据框列的字符串值看起来像字典列表吗?,string,pandas,list,dataframe,dictionary,String,Pandas,List,Dataframe,Dictionary,我有一个dataframe列,其中包含字符串值(已编辑) 数据帧列的类型为字符串,但(已编辑) 其值类似于字典列表(已编辑) 如何从该字符串中提取一些键值? 此字符串值类似于字典列表 当键“job”的值为“Director”时,如何提取name键的值 [{ 'credit_id': '549e9edcc3a3682f2300824b', 'department': 'Camera', 'gender': 2, 'id': 473,
name
键的值
[{
'credit_id': '549e9edcc3a3682f2300824b',
'department': 'Camera',
'gender': 2,
'id': 473,
'job': 'Additional Photography',
'name': 'Brian Tufano',
'profile_path': None
}, {
'credit_id': '52fe4214c3a36847f8002595',
'department': 'Directing',
'gender': 2,
'id': 578,
'job': 'Director',
'name': 'Ridley Scott',
'profile_path': '/oTAL0z0vsjipCruxXUsDUIieuhk.jpg'
}, {
'credit_id': '52fe4214c3a36847f800259b',
'department': 'Production',
'gender': 2,
'id': 581,
'job': 'Producer',
'name': 'Michael Deeley',
'profile_path': None
}, {
'credit_id': '52fe4214c3a36847f800263f',
'department': 'Writing',
'gender': 2,
'id': 584,
'job': 'Novel',
'name': 'Philip K. Dick',
'profile_path': '/jDOKJN8SQ17QsJ7omv4yBNZi7XY.jpg'
}, {
'credit_id': '549e9f85c3a3685542004c7b',
'department': 'Crew',
'gender': 2,
'id': 584,
'job': 'Thanks',
'name': 'Philip K. Dick',
'profile_path': '/jDOKJN8SQ17QsJ7omv4yBNZi7XY.jpg'
}, {
'credit_id': '52fe4214c3a36847f800261b',
'department': 'Writing',
'gender': 2,
'id': 583,
'job': 'Screenplay',
'name': 'Hampton Fancher',
'profile_path': '/lrGecnLhzjzgwjKHvrmYtRAqOsP.jpg'
}
]
您可以通过将列表转换为数据帧来实现这一点: 或者还有一种方法是使用.loc[]
df.loc[df["job"]=="Director",["name"]] # You will get a dataframe with name column having all the names matching the condition.
欢迎来到StackOverflow!请不要只提供代码-尝试解释它的功能。您还应该使用多行代码框(三个反勾号)。在@finnmglas的评论上展开,纯代码答案可能对OP有所帮助,但它们通常对后来遇到类似问题的人没有帮助。
df.loc[df["job"]=="Director",["name"]] # You will get a dataframe with name column having all the names matching the condition.
import pandas as pd
df =pd.DataFrame(LIST)
Name=df['name'][df['job']=="Director"]
print('Name =',Name)