【问题标题】:Calling a data frame via a string通过字符串调用数据框
【发布时间】: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


    【解决方案1】:

    您的循环不会修改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)
    

    【讨论】:

    • 感谢您的洞察力。我明白了,但不知何故我看不到merged_df。
    猜你喜欢
    • 1970-01-01
    • 2018-12-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-10
    • 2020-11-08
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多