简体   繁体   English

python dataframe水平附加列

[英]python dataframe appending columns horizontally

I am trying to make a simple script that concatenates or appends multiple column sets that I pull from xls files within a directory. 我正在尝试制作一个简单的脚本来连接或附加从目录中的xls文件中提取的多个列集。 Each xls file has a format of: 每个xls文件的格式为:

Index    Exp. m/z   Intensity   
1        1000.11    1000
2        2000.14    2000
3        3000.15    3000

Each file has varying number of indices. 每个文件具有不同数量的索引。 Below is my code: 下面是我的代码:

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)

This produces an attributerror: DataFrame object has no attribute concat. 这将产生一个attributerror:DataFrame对象没有属性concat。 I also tried using append like: 我也尝试使用append:

data = df.append(df, axis=1) 

but I know that append has no axis keyword argument. 但我知道append没有axis关键字参数。 df.append(df) does work, but it places the columns at the bottom. df.append(df)确实有效,但是它将列放在底部。 I want something like: 我想要类似的东西:

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

and so on. 等等。 So the column sets that I pull from each file should be placed to the right of the previous column sets, with a column space in between. 因此,我从每个文件中提取的列集应放置在前一个列集的右侧,并在其间留有一个列间隔。

I think you need append DataFrames to list and then pd.concat : 我认为您需要append DataFrames到列表,然后添加pd.concat

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM