【发布时间】:2016-09-20 14:47:31
【问题描述】:
我有一个国家列表,例如:
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没有调用名称在国家列表中的数据框。如何从国家列表开始调用这些数据框?
谢谢各位大师!
【问题讨论】:
标签: python-3.x dataframe