1 回答

TA貢獻(xiàn)1818條經(jīng)驗(yàn) 獲得超7個贊
這是一種更有效的方法。您首先從字典中構(gòu)建一個數(shù)據(jù)框,然后在該數(shù)據(jù)框上執(zhí)行實(shí)際工作。
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
添加回答
舉報(bào)