.append 和 pd.concat() 的问题

Problems with .append and 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!

欢迎来到 SO。在您的代码中,您混淆了两件事。 您的 dfpandas.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)

我认为错误是由于 Python's native list append and pandas.DataFrame.append 之间的混淆造成的。我会稍微评论一下代码:

def load_all_csv(files_names):
   all_scenarios = []
   for files in files_names: #The name should match the parameter in the function.
        df = pd.read_csv(files, index_col='Month') #Read each file
        all_scenarios.append(df) #Create a list with all the dataframes previously read
   concat_dfs = pd.concat(all_scenarios, axis=1) #Concatenate all dfs
   return concat_dfs #Returns the concatenation of all dfs as a single dataframe
all_data = load_all_csv(files_in_list)