【发布时间】:2021-02-22 13:41:37
【问题描述】:
我是一个超级初学者,这是我的第一个问题。 我有这段代码,我尝试使用 .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!
【问题讨论】:
-
您正在使用 python 的原生函数
append()将单个元素添加到列表中,参数ignore_index不存在,应该删除。请不要将python的列表附加与pandas.DataFrame.appendpandas.pydata.org/pandas-docs/stable/reference/api/…混淆
标签: python pandas function append concat