我的数据框中的某些列由字典本身组成,例如这个数据框:
df = pd.DataFrame({'Name': ['Alice', 'Bob', 'Aritra'],
'Age': [25, 30, 35],
'Location': [
{'City': 'Seattle', 'State': 'WA'},
{'City': 'New York', 'State': 'NY'},
{'City': 'Albany', 'State': 'NY'}
]
})
df
Name Age Location
0 Alice 25 {'City': 'Seattle', 'State': 'WA'}
1 Bob 30 {'City': 'New York', 'State': 'NY'}
2 Aritra 35 {'City': 'Albany', 'State': 'NY'}
如何根据该字典中的值过滤数据框?
当我只想要一个值时,我可以这样做:
df['Location'][0]['State']
'WA'
但问题是列名和字典键之间需要索引。因此,像
df[df['Location']['State'] == 'NY']
这样选择所有来自纽约的人是行不通的。
有没有办法包含任何索引,或者必须以其他方式这样做?
期望的输出是
Name Age Location
1 Bob 30 {'City': 'New York', 'State': 'NY'}
2 Aritra 35 {'City': 'Albany', 'State': 'NY'}