python - .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!
解决方案
欢迎来到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)
推荐阅读
- php - PHP Session 不适用于某些(有时)移动用户?
- php - Laravel Cashier 10 未通过作曲家安装
- ios - 如何限制应用程序仅在 iphone 中下载而不在 iPad 中下载
- php - Octobercms - 表单上的 CSRF 保护以防止多次提交
- ios - 将 Xcode 更新到 10.4 版后 Apple Pay 无法正常工作
- java - 具有不同单元格高度的列表视图
- database - SQL * PLUS 在 glogin.sql 文件中定义 CONNECT_IDENTIFIER
- python-3.x - 在 Windows 10 上安装 fbprophet Python 时出现 Numpy 错误
- excel - 计算表中的期望 - 具有可变数量的条目
- ios - 表情符号肤色检测