Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jsf-2/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python .append和pd.concat()的问题_Python_Pandas_Function_Append_Concat - Fatal编程技术网

Python .append和pd.concat()的问题

Python .append和pd.concat()的问题,python,pandas,function,append,concat,Python,Pandas,Function,Append,Concat,我是个超级初学者,这是我的第一个问题。 我有一段代码,其中我尝试使用.append和pd.concat()添加几个df,使用这些选项是练习的一部分,但它给了我很多麻烦,最后一个是下一个错误: files_in_list= os.listdir("/content/gdrive/My Drive/simulation_data") 打印(列表中的文件)``` def load_all_csv(文件名): #这里的评论。。。 所有场景=[] 对于文件列表中的文件: df=pd.

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

files_in_list= os.listdir("/content/gdrive/My Drive/simulation_data")
打印(列表中的文件)```
def load_all_csv(文件名):
#这里的评论。。。
所有场景=[]
对于文件列表中的文件:
df=pd.read\u csv(文件、索引\u col='Month')
附加(df,ignore\u index=True)
局部放电浓度(所有情况下,轴=1)
返回
所有场景
所有数据=加载所有csv(文件列表中的文件)
```打印(所有数据)```
____________________________________________________________________________
TypeError回溯(最近一次调用上次)
在()
1#这里有评论
2#这里有评论
---->3所有数据=加载所有csv(文件列表中的文件)
4打印(所有数据)
加载所有csv(文件名)
4对于文件列表中的文件:
5 df=pd.read\u csv(文件、索引\u col='Month')
---->6所有场景。追加(df,忽略索引=True)
7 pd.concat(所有情况下,轴=1)
8返回
TypeError:append()不接受关键字参数
___________________________________________________________
我也尝试过在函数外使用循环,但它返回的是类似于示例的内容,而不是具有相同索引“Month”的df和每个文件/场景的一列。
场景-空调时间表
月
一月五日六一
二月六日五十分
三月九日七时三十分
一九九五年四月十一日
五月十六日五十二分
六月十八日
七月二十二日十三日
八月二十二日十四分
九月二十日三十八分
十月十五日
十一月十一日
12月7日16日,场景-凉爽的屋顶
月
一月四日四十六分
二月五日三十九分
一九九六年三月八日
四月十一日
五月十七日二十八分
六月二十日五十四分
七月二十四日
1997年8月24日。。。
我需要函数在一个数据框中为我提供数据,该数据框有一个12个月的索引,其余信息在每个文件/场景的单独列中。
欢迎任何帮助!

欢迎来到SO。在你的代码中,你混淆了两件事。 您的
df
是一个
pandas.DataFrame
,而您的
all\u场景
是一个python内置的
列表
。即使两者都实现了
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)


我相信这个错误是由于和之间的混淆造成的。我将对代码稍加注释:

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)

如果您使用python的本机函数
append()
,该函数将单个元素添加到列表中,那么参数
ignore\u index
不存在,应该删除。请不要将python的列表append与熊猫.DataFrame.append混为一谈@CeliusStingher感谢您的输入,我在代码中更正了它
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)