Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/278.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数据框水平追加列_Python_Pandas_Dataframe_Append_Concat - Fatal编程技术网

python数据框水平追加列

python数据框水平追加列,python,pandas,dataframe,append,concat,Python,Pandas,Dataframe,Append,Concat,我试图创建一个简单的脚本,将从目录中的xls文件中提取的多个列集连接或附加在一起。每个xls文件的格式为: Index Exp. m/z Intensity 1 1000.11 1000 2 2000.14 2000 3 3000.15 3000 每个文件都有不同数量的索引。下面是我的代码: import pandas as pd import os import tkinter.filedialog full_pa

我试图创建一个简单的脚本,将从目录中的xls文件中提取的多个列集连接或附加在一起。每个xls文件的格式为:

Index    Exp. m/z   Intensity   
1        1000.11    1000
2        2000.14    2000
3        3000.15    3000
每个文件都有不同数量的索引。下面是我的代码:

import pandas as pd
import os
import tkinter.filedialog

full_path = tkinter.filedialog.askdirectory(initialdir='.')
os.chdir(full_path)

data = {}
df = pd.DataFrame()

for files in os.listdir(full_path):
    if os.path.isfile(os.path.join(full_path, files)):
        df = pd.read_excel(files, 'Sheet1')[['Exp. m/z', 'Intensity']]
        data = df.concat(df, axis=1)

data.to_excel('test.xls', index=False)
这会产生AttributeError:DataFrame对象没有属性concat。我还尝试使用append,比如:

data = df.append(df, axis=1) 
但我知道append没有axis关键字参数。append(df)确实有效,但它将列放在底部。我想要像这样的东西:

Exp. m/z   Intensity       Exp. m/z   Intensity  
1000.11    1000            1001.43    1000
2000.14    2000            1011.45    2000
3000.15    3000

等等。因此,我从每个文件中提取的列集应该放在前面的列集的右侧,中间有一个列空间

我认为您需要
将数据帧添加到列表中,然后:


仅输入错误-不是
df.concat(df,axis=1)
而是
pd.concat(df,axis=1)
谢谢!我想我已经习惯了附加/连接,但从来没有想过这样做。一个简单的问题:有没有一种方法可以在连接时在文件之间添加空列?我认为最简单的方法是在附加之前在循环中添加它(dfs),我编辑答案。
dfs = []
for files in os.listdir(full_path):
    if os.path.isfile(os.path.join(full_path, files)):
        df = pd.read_excel(files, 'Sheet1')[['Exp. m/z', 'Intensity']]
        #for add empty column 
        df['empty'] = np.nan
        dfs.append(df)
data = pd.concat(dfs, axis=1)