1 回答
TA贡献1818条经验 获得超7个赞
这是一种更有效的方法。您首先从字典中构建一个数据框,然后在该数据框上执行实际工作。
single_df = pd.concat([df.assign(name = k) for k, df in names_and_places.items()])
single_df["Key"] = single_df.Key.replace("NAN", np.NaN)
single_df.dropna(inplace=True)
# Since the location is a string, we have to parse it.
location_df = pd.DataFrame(single_df.Key.str.replace(r"[\[\]]", "").str.split(",", expand=True))
location_df.columns = ["Country", "State", "County", "City"]
single_df = pd.concat([single_df, location_df], axis=1)
# this is where the actual query goes.
single_df[(single_df.Country == "USA") & (single_df.State == "CT")].name
输出是:
2 Brett
2 Claire
2 Dane
2 Edward
Name: name, dtype: object
添加回答
举报