在数据框中访问字典

时间:2018-11-27 03:37:31

标签: python

我有以下数据框(具有1324行): enter image description here

我需要弄清楚哪些城市适合外卖('Take_out':属性字典中为True)

2 个答案:

答案 0 :(得分:1)

要获得此答案,我首先创建了一个虚拟DataFrame进行测试:

import numpy as np
import pandas as pd
# create a dictionary list
d = list({'Take-out': True} for x in np.arange(10))
ddf = pd.Series(d, name='attributes')
ddf = pd.DataFrame(ddf)
ddf.index.name = 'cities'
print(ddf)

这将提供与图像中相似的DataFrame。

接下来,遍历DataFrame,像这样访问“属性”列:

# cities buffer will hold successes
cities = []
# iterate over the list of dictionaries:
for i, each in enumerate(ddf['attributes']):
    # check if the keys is in that dictionary, if so, keep the city name
    if 'Take-out' in ddf['attributes'][i].keys():
        # the index is named 'cities' and each position is a city name, so:
        cities.append(ddf.index[i])
print(cities)

答案 1 :(得分:0)

字典并不是您真正想要存储在DataFrame中的字典,但是您可以尝试:

df = df.assign(**df.attributes.dropna().apply(pd.Series))
df[df["Take_out"] == True]

这与此处描述的想法大致相同:Unpack dictionary from Pandas Column。解压缩该字典后,我们可以像通常一样选择带有“ Take_out” == True的行。