gpt4 book ai didi

python - 访问数据框内的字典

转载 作者:行者123 更新时间:2023-12-01 01:24:18 34 4
gpt4 key购买 nike

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

我需要找出哪些城市适合外卖(属性字典中'Take_out': True)

最佳答案

为了得到这个答案,我首先创建了一个虚拟 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)

关于python - 访问数据框内的字典,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53492365/

34 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com