首页 > 解决方案 > .append 和 pd.concat() 的问题

问题描述

我是一个超级初学者,这是我的第一个问题。我有这段代码,我尝试使用 .append 和 pd.concat() 添加几个 df,使用这些选项是练习的一部分,但它给我带来了很多麻烦,最后一个是下一个错误:

files_in_list= os.listdir("/content/gdrive/My Drive/simulation_data")
print(files_in_list)``` 

    def load_all_csv(files_names):
    # Comments here ...
    all_scenarios = []
    for files in files_in_list:
      df = pd.read_csv(files, index_col='Month')
      all_scenarios.append(df, ignore_index=True)
      pd.concat(all_scenarios, axis=1)
    return
    all_scenarios

    all_data = load_all_csv(files_in_list)
```print(all_data)```
____________________________________________________________________________
 TypeError                                 Traceback (most recent call last)
<ipython-input-43-10abf3efc373> in <module>()
      1 # Comments here
      2 # Comments here
----> 3 all_data = load_all_csv(files_in_list)
      4 print(all_data)

<ipython-input-42-0e449e23623a> in load_all_csv(files_names)
      4     for files in files_in_list:
      5       df = pd.read_csv(files, index_col='Month')
----> 6       all_scenarios.append(df, ignore_index=True)
      7       pd.concat(all_scenarios, axis=1)
      8     return

TypeError: append() takes no keyword arguments
___________________________________________________________

I've also tried with the loop outside of the function but it returned something like the example instead of a df with the same index 'Month'and a column for each of the files/scenarios.

           Scenario - Aircon Schedules
Month                                 
January                           5.61
February                          6.50
March                             9.70
April                            11.95
May                              16.52
June                             18.89
July                             22.13
August                           22.14
September                        20.38
October                          15.87
November                         11.71
December                          7.16,            Scenario - Cool roof
Month                          
January                    4.46
February                   5.39
March                      8.96
April                     11.73
May                       17.28
June                      20.54
July                      24.76
August                    24.97... ... ...

I need the function to give me the data in a data frame that has a 12-month index and the rest of the info in separate columns for each file/scenario.  
 
Any help will be most welcome!

标签: pythonpandasfunctionappendconcat

解决方案


欢迎来到SO。在您的代码中,您混淆了两件事。你df是一个pandas.DataFrame并且你all_scenarios是一个python内置的list。即使两者都实现了该append函数,该列表也不会采用错误消息中所述的附加参数。下面的代码稍微更正了,因为在循环中,只有数据帧被创建并附加到列表中,然后连接起来。

def load_all_csv(file_names):
    all_scenarios = []
    for file_name in file_names:
      df = pd.read_csv(file_name)
      all_scenarios.append(df)
    all_scenarios = pd.concat(all_scenarios, axis=1)
    return all_scenarios

files_in_list= os.listdir("/content/gdrive/My Drive/simulation_data")
all_data = load_all_csv(files_in_list)


推荐阅读