我有一系列国家/地区,例如:
country = ["Brazil", "Chile", "Colombia", "Mexico", "Panama", "Peru", "Venezuela"]
我使用国家/地区列表中的名称创建了数据框:
for c in country:
c = pd.read_excel(str(c + ".xls"), skiprows = 1)
c = pd.to_datetime(c.Date, infer_datetime_format=True)
c = c[["Date", "spreads"]]
现在,我希望能够使用日期列作为键来合并所有国家/地区的数据框。我们的想法是创建一个如下所示的循环:
df = Brazil #this is the first dataframe, which also corresponds to the first element of the list country.
for i in range(len(country)-1):
df = df.merge(country[i+1], on = "Date", how = "inner")
df.set_index("Date", inplace=True)
我收到错误ValueError: can not merge DataFrame with instance of type <class 'str'>
。似乎python没有调用名称在国家/地区列表中的数据框。如何从国家/地区列表中调用这些数据框?
谢谢大师!
答案 0 :(得分:0)
您的循环不会修改country
列表的内容,因此country
仍然是字符串列表。
考虑构建一个新的数据框列表并循环遍历:
country_dfs = []
for c in country:
df = pd.read_excel(c + ".xls", skiprows=1)
df = pd.to_datetime(df.Date, infer_datetime_format=True)
df = df[["Date", "spreads"]]
# add new dataframe to our list of dataframes
country_dfs.append(df)
然后合并,
merged_df = country_dfs[0]
for df in country_dfs[1:]:
merged_df = merged_df.merge(df, on='Date', how='inner')
merged_df.set_index('Date', inplace=True)