Pandas文件合并

快速合并文件

import pandas as pd
import os
import glob
# 根据需要修改以下部分
path = os.path.abspath('../实验组/孙奶奶/')
csv_list = glob.glob('../实验组/孙奶奶/*Fix.csv')
new_file_name = 'data.csv'  # 合并后的文件名
cols_new_name = [ 'PosX', 'PosY', 'StartTimestamp', 'Length', 'Distance', 'Orientation', 'Velocity']  # 汇总后的列名,根据需要修改
cols_num = [ 2, 3, 4, 5, 6, 7, 8]  # 需要合并的列的索引,从0开始
csv_list
['../实验组/孙奶奶\\20210224112557-1-GazeFix.csv',
 '../实验组/孙奶奶\\20210224112655-1-GazeFix.csv',
 '../实验组/孙奶奶\\20210224112740-1-GazeFix.csv',
 '../实验组/孙奶奶\\20210224112816-1-GazeFix.csv',
 '../实验组/孙奶奶\\20210224112900-1-GazeFix.csv',
 '../实验组/孙奶奶\\20210224112931-1-GazeFix.csv',
 '../实验组/孙奶奶\\20210224113007-1-GazeFix.csv',
 '../实验组/孙奶奶\\20210224113041-1-GazeFix.csv']
file_allname = []  # 用于存储全部文件的名字
for filename in csv_list:
    t=filename.split("\\")[1]
    file_allname.append(t)  # 拼接.csv后缀,生成完整文件名
file_allname
['20210224112557-1-GazeFix.csv',
 '20210224112655-1-GazeFix.csv',
 '20210224112740-1-GazeFix.csv',
 '20210224112816-1-GazeFix.csv',
 '20210224112900-1-GazeFix.csv',
 '20210224112931-1-GazeFix.csv',
 '20210224113007-1-GazeFix.csv',
 '20210224113041-1-GazeFix.csv']
df = pd.DataFrame(cols_new_name).T
try:
    print('开始合并:')
    df.to_csv(path + '/' + new_file_name, encoding='gbk', header=False, index=False)
    for fn in file_allname:
        data = pd.read_csv(path + '/' + fn)
        print('合并' + fn)
        data = data.iloc[2:, cols_num]  # 跳过标题行
        data.to_csv(path + '/' + new_file_name, mode='a', encoding='gbk', header=False, index=False)
    print('合并结束,生成新文件:' + new_file_name)
except PermissionError as e:
    print('出现异常:' + str(type(e)) + '!\n文件已打开?请先关闭')
开始合并:
合并20210224112557-1-GazeFix.csv
合并20210224112655-1-GazeFix.csv
合并20210224112740-1-GazeFix.csv
合并20210224112816-1-GazeFix.csv
合并20210224112900-1-GazeFix.csv
合并20210224112931-1-GazeFix.csv
合并20210224113007-1-GazeFix.csv
合并20210224113041-1-GazeFix.csv
合并结束,生成新文件:data.csv
import numpy as np
data = pd.read_csv(r'../实验组/孙奶奶/data.csv')
print(data.columns)#获取列索引值
data1 = 1#获取列名为flow的数据作为新列的数据
data['experiment'] = data1 #将新列的名字设置为cha
data.to_csv(r"../实验组/孙奶奶/data1.csv",mode = 'a',index =False)
#mode=a,以追加模式写入,header表示列名,默认为true,index表示行名,默认为true,再次写入不需要行名
print(data)
Index(['PosX', 'PosY', 'StartTimestamp', 'Length', 'Distance', 'Orientation',
       'Velocity'],
      dtype='object')
         PosX      PosY  StartTimestamp     Length  Distance  Orientation  \
0    912.0407  519.0507        731.2565  259.20290  32.07658   128.300200   
1    932.3488  505.6841       1013.2470  102.34260  17.75659    33.109150   
2    876.8053  595.2438       1486.7100  213.80040  22.53919    39.154030   
3    915.7766  639.3555       1824.6460   56.01120  17.04834   144.840700   
4    960.8786  579.5919       2014.5590  144.50450  33.84498   115.930700   
..        ...       ...             ...        ...       ...          ...   
158  769.8637  501.5744       7883.0490  144.74490  28.36261    77.234700   
159  881.5404  536.4966       8273.4380   11.31439  18.58091    74.448220   
160  811.8618  542.0256       8796.8970  269.73720  28.85307     9.199452   
161  821.8842  585.4316       9166.6420   99.99847  30.84698    18.533490   
162  769.6884  757.5984       9533.9580   66.70380  16.39015   174.827900   

     Velocity  experiment  
0    1.407664           1  
1    0.047846           1  
2    0.181569           1  
3    0.127320           1  
4    1.522873           1  
..        ...         ...  
158  0.115462           1  
159  0.036281           1  
160  0.288513           1  
161  0.115394           1  
162  0.056674           1  

[163 rows x 8 columns]
import os,shutil
from tqdm import tqdm  # 加了个进度条
sourceDir=r"../对照组/"
targetDir=r"../对照组/total"
for root, dirs, files in os.walk(sourceDir):
    for file in tqdm(files,desc="Waiting..."):
        shutil.copy(os.path.join(root,file),targetDir) #执行复制操作
Waiting...: 0it [00:00, ?it/s]
Waiting...: 0it [00:00, ?it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 32/32 [00:00<00:00, 359.14it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 35/35 [00:00<00:00, 368.55it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 51/51 [00:00<00:00, 343.11it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 45/45 [00:00<00:00, 436.91it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 33/33 [00:00<00:00, 340.20it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 32/32 [00:00<00:00, 380.95it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 24/24 [00:00<00:00, 800.03it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 31/31 [00:00<00:00, 418.92it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 32/32 [00:00<00:00, 380.61it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 32/32 [00:00<00:00, 400.05it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 29/29 [00:00<00:00, 371.78it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 22/22 [00:00<00:00, 407.42it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 38/38 [00:00<00:00, 379.93it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 24/24 [00:00<00:00, 300.00it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 38/38 [00:00<00:00, 347.73it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 36/36 [00:00<00:00, 330.81it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 32/32 [00:00<00:00, 319.75it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 32/32 [00:00<00:00, 326.04it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 32/32 [00:00<00:00, 350.88it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 44/44 [00:00<00:00, 394.12it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 36/36 [00:00<00:00, 401.59it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 54/54 [00:00<00:00, 439.52it/s]
Waiting...: 100%|█████████████████████████████████████████████████████████████████████| 22/22 [00:00<00:00, 366.32it/s]
import pandas as pd
import os
import glob
# 根据需要修改以下部分
path = os.path.abspath('../对照组/total/')
csv_list = glob.glob('../对照组/total/*Fix.csv')
new_file_name = 'data.csv'  # 合并后的文件名
cols_new_name = [ 'PosX', 'PosY', 'StartTimestamp', 'Length', 'Distance', 'Orientation', 'Velocity']  # 汇总后的列名,根据需要修改
cols_num = [ 2, 3, 4, 5, 6, 7, 8]  # 需要合并的列的索引,从0开始
csv_list
['../对照组/total\\20210223093450-1-GazeFix.csv',
 '../对照组/total\\20210223093646-1-GazeFix.csv',
 '../对照组/total\\20210223093846-1-GazeFix.csv',
 '../对照组/total\\20210223094028-1-GazeFix.csv',
 '../对照组/total\\20210223094558-11-GazeFix.csv',
 '../对照组/total\\20210223094657-1-GazeFix.csv',
 '../对照组/total\\20210223094813-1-GazeFix.csv',
 '../对照组/total\\20210223094859-1-GazeFix.csv',
 '../对照组/total\\20210223094944-1-GazeFix.csv',
 '../对照组/total\\20210223095848-1-GazeFix.csv',
 '../对照组/total\\20210223095956-1-GazeFix.csv',
 '../对照组/total\\20210223100052-1-GazeFix.csv',
 '../对照组/total\\20210223100143-1-GazeFix.csv',
 '../对照组/total\\20210223100231-1-GazeFix.csv',
 '../对照组/total\\20210223100325-1-GazeFix.csv',
 '../对照组/total\\20210223100426-1-GazeFix.csv',
 '../对照组/total\\20210223100523-1-GazeFix.csv',
 '../对照组/total\\20210223100707-1-GazeFix.csv',
 '../对照组/total\\20210223100806-1-GazeFix.csv',
 '../对照组/total\\20210223100901-1-GazeFix.csv',
 '../对照组/total\\20210223100950-1-GazeFix.csv',
 '../对照组/total\\20210223101034-1-GazeFix.csv',
 '../对照组/total\\20210223101116-1-GazeFix.csv',
 '../对照组/total\\20210223101201-1-GazeFix.csv',
 '../对照组/total\\20210223101255-1-GazeFix.csv',
 '../对照组/total\\20210223101546-1-GazeFix.csv',
 '../对照组/total\\20210223101704-1-GazeFix.csv',
 '../对照组/total\\20210223101736-1-GazeFix.csv',
 '../对照组/total\\20210223101822-1-GazeFix.csv',
 '../对照组/total\\20210223101857-1-GazeFix.csv',
 '../对照组/total\\20210223104246-1-GazeFix.csv',
 '../对照组/total\\20210223104350-1-GazeFix.csv',
 '../对照组/total\\20210223104438-1-GazeFix.csv',
 '../对照组/total\\20210223104518-1-GazeFix.csv',
 '../对照组/total\\20210223104559-1-GazeFix.csv',
 '../对照组/total\\20210223104640-1-GazeFix.csv',
 '../对照组/total\\20210223104717-1-GazeFix.csv',
 '../对照组/total\\20210223104755-1-GazeFix.csv',
 '../对照组/total\\20210223104825-1-GazeFix.csv',
 '../对照组/total\\20210223104858-1-GazeFix.csv',
 '../对照组/total\\20210223153927-1-GazeFix.csv',
 '../对照组/total\\20210223154037-1-GazeFix.csv',
 '../对照组/total\\20210223154113-1-GazeFix.csv',
 '../对照组/total\\20210223154211-1-GazeFix.csv',
 '../对照组/total\\20210223154257-1-GazeFix.csv',
 '../对照组/total\\20210223154336-1-GazeFix.csv',
 '../对照组/total\\20210223154411-1-GazeFix.csv',
 '../对照组/total\\20210223154450-1-GazeFix.csv',
 '../对照组/total\\20210223154551-1-GazeFix.csv',
 '../对照组/total\\20210223154751-1-GazeFix.csv',
 '../对照组/total\\20210223154837-1-GazeFix.csv',
 '../对照组/total\\20210223154913-1-GazeFix.csv',
 '../对照组/total\\20210223154947-1-GazeFix.csv',
 '../对照组/total\\20210223155024-1-GazeFix.csv',
 '../对照组/total\\20210223155057-1-GazeFix.csv',
 '../对照组/total\\20210223155137-1-GazeFix.csv',
 '../对照组/total\\20210223155302-1-GazeFix.csv',
 '../对照组/total\\20210223155420-1-GazeFix.csv',
 '../对照组/total\\20210223155505-1-GazeFix.csv',
 '../对照组/total\\20210223155540-1-GazeFix.csv',
 '../对照组/total\\20210223155615-1-GazeFix.csv',
 '../对照组/total\\20210223155652-1-GazeFix.csv',
 '../对照组/total\\20210223155732-1-GazeFix.csv',
 '../对照组/total\\20210223155811-1-GazeFix.csv',
 '../对照组/total\\20210223155927-1-GazeFix.csv',
 '../对照组/total\\20210223160123-1-GazeFix.csv',
 '../对照组/total\\20210223160207-1-GazeFix.csv',
 '../对照组/total\\20210223160243-1-GazeFix.csv',
 '../对照组/total\\20210223160318-1-GazeFix.csv',
 '../对照组/total\\20210223160400-1-GazeFix.csv',
 '../对照组/total\\20210223160439-1-GazeFix.csv',
 '../对照组/total\\20210223160514-1-GazeFix.csv',
 '../对照组/total\\20210223161645-1-GazeFix.csv',
 '../对照组/total\\20210223161754-1-GazeFix.csv',
 '../对照组/total\\20210223161837-1-GazeFix.csv',
 '../对照组/total\\20210223161914-1-GazeFix.csv',
 '../对照组/total\\20210223161953-1-GazeFix.csv',
 '../对照组/total\\20210223162034-1-GazeFix.csv',
 '../对照组/total\\20210223162136-1-GazeFix.csv',
 '../对照组/total\\20210223162210-1-GazeFix.csv',
 '../对照组/total\\20210223162250-1-GazeFix.csv',
 '../对照组/total\\20210224104250-1-GazeFix.csv',
 '../对照组/total\\20210224104333-1-GazeFix.csv',
 '../对照组/total\\20210224104413-1-GazeFix.csv',
 '../对照组/total\\20210224104454-1-GazeFix.csv',
 '../对照组/total\\20210224104534-1-GazeFix.csv',
 '../对照组/total\\20210224104609-1-GazeFix.csv',
 '../对照组/total\\20210224104649-1-GazeFix.csv',
 '../对照组/total\\20210224104733-1-GazeFix.csv',
 '../对照组/total\\20210224104902-1-GazeFix.csv',
 '../对照组/total\\20210224104957-1-GazeFix.csv',
 '../对照组/total\\20210224105032-1-GazeFix.csv',
 '../对照组/total\\20210224105105-1-GazeFix.csv',
 '../对照组/total\\20210224105153-1-GazeFix.csv',
 '../对照组/total\\20210224105227-1-GazeFix.csv',
 '../对照组/total\\20210224105302-1-GazeFix.csv',
 '../对照组/total\\20210224105336-1-GazeFix.csv',
 '../对照组/total\\20210224113307-1-GazeFix.csv',
 '../对照组/total\\20210224113355-1-GazeFix.csv',
 '../对照组/total\\20210224113441-1-GazeFix.csv',
 '../对照组/total\\20210224113516-1-GazeFix.csv',
 '../对照组/total\\20210224113552-1-GazeFix.csv',
 '../对照组/total\\20210224113631-0-GazeFix.csv',
 '../对照组/total\\20210224113707-1-GazeFix.csv',
 '../对照组/total\\20210224113746-1-GazeFix.csv',
 '../对照组/total\\20210224145302-1-GazeFix.csv',
 '../对照组/total\\20210224145408-1-GazeFix.csv',
 '../对照组/total\\20210224145453-1-GazeFix.csv',
 '../对照组/total\\20210224145539-1-GazeFix.csv',
 '../对照组/total\\20210224145624-1-GazeFix.csv',
 '../对照组/total\\20210224145701-1-GazeFix.csv',
 '../对照组/total\\20210224145745-10-GazeFix.csv',
 '../对照组/total\\20210224145824-1-GazeFix.csv',
 '../对照组/total\\20210316075937-1-GazeFix.csv',
 '../对照组/total\\20210316080052-1-GazeFix.csv',
 '../对照组/total\\20210316080157-1-GazeFix.csv',
 '../对照组/total\\20210316080235-1-GazeFix.csv',
 '../对照组/total\\20210316080318-1-GazeFix.csv',
 '../对照组/total\\20210316080403-1-GazeFix.csv',
 '../对照组/total\\20210316080448-1-GazeFix.csv',
 '../对照组/total\\20210316080542-1-GazeFix.csv',
 '../对照组/total\\20210316080624-1-GazeFix.csv',
 '../对照组/total\\20210316080716-1-GazeFix.csv',
 '../对照组/total\\20210316080748-1-GazeFix.csv',
 '../对照组/total\\20210316080857-1-GazeFix.csv',
 '../对照组/total\\20210316080943-1-GazeFix.csv',
 '../对照组/total\\20210316081019-1-GazeFix.csv',
 '../对照组/total\\20210316081052-1-GazeFix.csv',
 '../对照组/total\\20210316081124-1-GazeFix.csv',
 '../对照组/total\\20210316081207-1-GazeFix.csv',
 '../对照组/total\\20210316081249-1-GazeFix.csv',
 '../对照组/total\\20210316081352-1-GazeFix.csv',
 '../对照组/total\\20210316082700-1-GazeFix.csv',
 '../对照组/total\\20210316082737-1-GazeFix.csv',
 '../对照组/total\\20210316082814-1-GazeFix.csv',
 '../对照组/total\\20210316082847-1-GazeFix.csv',
 '../对照组/total\\20210316082921-1-GazeFix.csv',
 '../对照组/total\\20210316083001-1-GazeFix.csv',
 '../对照组/total\\20210316083040-1-GazeFix.csv',
 '../对照组/total\\20210316083114-1-GazeFix.csv',
 '../对照组/total\\20210316083147-1-GazeFix.csv',
 '../对照组/total\\20210316083232-1-GazeFix.csv',
 '../对照组/total\\20210316083307-1-GazeFix.csv',
 '../对照组/total\\20210316083337-1-GazeFix.csv',
 '../对照组/total\\20210316083526-1-GazeFix.csv',
 '../对照组/total\\20210316084843-1-GazeFix.csv',
 '../对照组/total\\20210316085010-1-GazeFix.csv',
 '../对照组/total\\20210316085042-1-GazeFix.csv',
 '../对照组/total\\20210316085134-1-GazeFix.csv',
 '../对照组/total\\20210316085215-1-GazeFix.csv',
 '../对照组/total\\20210316085254-1-GazeFix.csv',
 '../对照组/total\\20210316085334-1-GazeFix.csv',
 '../对照组/total\\20210316085401-1-GazeFix.csv',
 '../对照组/total\\20210316085457-1-GazeFix.csv',
 '../对照组/total\\20210316091924-1-GazeFix.csv',
 '../对照组/total\\20210316092020-1-GazeFix.csv',
 '../对照组/total\\20210316092056-4-GazeFix.csv',
 '../对照组/total\\20210316092133-1-GazeFix.csv',
 '../对照组/total\\20210316092209-1-GazeFix.csv',
 '../对照组/total\\20210316092242-1-GazeFix.csv',
 '../对照组/total\\20210316092332-1-GazeFix.csv',
 '../对照组/total\\20210316092411-1-GazeFix.csv',
 '../对照组/total\\20210316092658-1-GazeFix.csv',
 '../对照组/total\\20210316092740-1-GazeFix.csv',
 '../对照组/total\\20210316092814-1-GazeFix.csv',
 '../对照组/total\\20210316092850-1-GazeFix.csv',
 '../对照组/total\\20210316092928-1-GazeFix.csv',
 '../对照组/total\\20210316093004-1-GazeFix.csv',
 '../对照组/total\\20210316093048-1-GazeFix.csv',
 '../对照组/total\\20210316093122-1-GazeFix.csv',
 '../对照组/total\\20210316094048-1-GazeFix.csv',
 '../对照组/total\\20210316094152-1-GazeFix.csv',
 '../对照组/total\\20210316094236-1-GazeFix.csv',
 '../对照组/total\\20210316094314-1-GazeFix.csv',
 '../对照组/total\\20210316094347-1-GazeFix.csv',
 '../对照组/total\\20210316094419-1-GazeFix.csv',
 '../对照组/total\\20210316094508-1-GazeFix.csv',
 '../对照组/total\\20210316094538-1-GazeFix.csv',
 '../对照组/total\\20210316102536-1-GazeFix.csv',
 '../对照组/total\\20210316102640-1-GazeFix.csv',
 '../对照组/total\\20210316102717-1-GazeFix.csv',
 '../对照组/total\\20210316102747-1-GazeFix.csv',
 '../对照组/total\\20210316102830-1-GazeFix.csv',
 '../对照组/total\\20210316102906-1-GazeFix.csv',
 '../对照组/total\\20210316102944-1-GazeFix.csv',
 '../对照组/total\\20210316103021-1-GazeFix.csv',
 '../对照组/total\\20210316103058-1-GazeFix.csv',
 '../对照组/total\\20210316103130-1-GazeFix.csv',
 '../对照组/total\\20210316103158-1-GazeFix.csv',
 '../对照组/total\\20210316112446-1-GazeFix.csv',
 '../对照组/total\\20210316112527-1-GazeFix.csv',
 '../对照组/total\\20210316112617-1-GazeFix.csv',
 '../对照组/total\\20210316112655-1-GazeFix.csv',
 '../对照组/total\\20210316112727-1-GazeFix.csv',
 '../对照组/total\\20210316112802-1-GazeFix.csv',
 '../对照组/total\\20210316112841-1-GazeFix.csv',
 '../对照组/total\\20210316112915-1-GazeFix.csv',
 '../对照组/total\\20210316112948-1-GazeFix.csv',
 '../对照组/total\\20210316113038-1-GazeFix.csv',
 '../对照组/total\\20210316113113-1-GazeFix.csv']
file_allname = []  # 用于存储全部文件的名字
for filename in csv_list:
    t=filename.split("\\")[1]
    file_allname.append(t)  # 拼接.csv后缀,生成完整文件名
df = pd.DataFrame(cols_new_name).T
try:
    print('开始合并:')
    df.to_csv(path + '/' + new_file_name, encoding='gbk', header=False, index=False)
    for fn in file_allname:
        data = pd.read_csv(path + '/' + fn)
        print('合并' + fn)
        data = data.iloc[2:, cols_num]  # 跳过标题行
        data.to_csv(path + '/' + new_file_name, mode='a', encoding='gbk', header=False, index=False)
    print('合并结束,生成新文件:' + new_file_name)
except PermissionError as e:
    print('出现异常:' + str(type(e)) + '!\n文件已打开?请先关闭')
开始合并:
合并20210223093450-1-GazeFix.csv
合并20210223093646-1-GazeFix.csv
合并20210223093846-1-GazeFix.csv
合并20210223094028-1-GazeFix.csv
合并20210223094558-11-GazeFix.csv
合并20210223094657-1-GazeFix.csv
合并20210223094813-1-GazeFix.csv
合并20210223094859-1-GazeFix.csv
合并20210223094944-1-GazeFix.csv
合并20210223095848-1-GazeFix.csv
合并20210223095956-1-GazeFix.csv
合并20210223100052-1-GazeFix.csv
合并20210223100143-1-GazeFix.csv
合并20210223100231-1-GazeFix.csv
合并20210223100325-1-GazeFix.csv
合并20210223100426-1-GazeFix.csv
合并20210223100523-1-GazeFix.csv
合并20210223100707-1-GazeFix.csv
合并20210223100806-1-GazeFix.csv
合并20210223100901-1-GazeFix.csv
合并20210223100950-1-GazeFix.csv
合并20210223101034-1-GazeFix.csv
合并20210223101116-1-GazeFix.csv
合并20210223101201-1-GazeFix.csv
合并20210223101255-1-GazeFix.csv
合并20210223101546-1-GazeFix.csv
合并20210223101704-1-GazeFix.csv
合并20210223101736-1-GazeFix.csv
合并20210223101822-1-GazeFix.csv
合并20210223101857-1-GazeFix.csv
合并20210223104246-1-GazeFix.csv
合并20210223104350-1-GazeFix.csv
合并20210223104438-1-GazeFix.csv
合并20210223104518-1-GazeFix.csv
合并20210223104559-1-GazeFix.csv
合并20210223104640-1-GazeFix.csv
合并20210223104717-1-GazeFix.csv
合并20210223104755-1-GazeFix.csv
合并20210223104825-1-GazeFix.csv
合并20210223104858-1-GazeFix.csv
合并20210223153927-1-GazeFix.csv
合并20210223154037-1-GazeFix.csv
合并20210223154113-1-GazeFix.csv
合并20210223154211-1-GazeFix.csv
合并20210223154257-1-GazeFix.csv
合并20210223154336-1-GazeFix.csv
合并20210223154411-1-GazeFix.csv
合并20210223154450-1-GazeFix.csv
合并20210223154551-1-GazeFix.csv
合并20210223154751-1-GazeFix.csv
合并20210223154837-1-GazeFix.csv
合并20210223154913-1-GazeFix.csv
合并20210223154947-1-GazeFix.csv
合并20210223155024-1-GazeFix.csv
合并20210223155057-1-GazeFix.csv
合并20210223155137-1-GazeFix.csv
合并20210223155302-1-GazeFix.csv
合并20210223155420-1-GazeFix.csv
合并20210223155505-1-GazeFix.csv
合并20210223155540-1-GazeFix.csv
合并20210223155615-1-GazeFix.csv
合并20210223155652-1-GazeFix.csv
合并20210223155732-1-GazeFix.csv
合并20210223155811-1-GazeFix.csv
合并20210223155927-1-GazeFix.csv
合并20210223160123-1-GazeFix.csv
合并20210223160207-1-GazeFix.csv
合并20210223160243-1-GazeFix.csv
合并20210223160318-1-GazeFix.csv
合并20210223160400-1-GazeFix.csv
合并20210223160439-1-GazeFix.csv
合并20210223160514-1-GazeFix.csv
合并20210223161645-1-GazeFix.csv
合并20210223161754-1-GazeFix.csv
合并20210223161837-1-GazeFix.csv
合并20210223161914-1-GazeFix.csv
合并20210223161953-1-GazeFix.csv
合并20210223162034-1-GazeFix.csv
合并20210223162136-1-GazeFix.csv
合并20210223162210-1-GazeFix.csv
合并20210223162250-1-GazeFix.csv
合并20210224104250-1-GazeFix.csv
合并20210224104333-1-GazeFix.csv
合并20210224104413-1-GazeFix.csv
合并20210224104454-1-GazeFix.csv
合并20210224104534-1-GazeFix.csv
合并20210224104609-1-GazeFix.csv
合并20210224104649-1-GazeFix.csv
合并20210224104733-1-GazeFix.csv
合并20210224104902-1-GazeFix.csv
合并20210224104957-1-GazeFix.csv
合并20210224105032-1-GazeFix.csv
合并20210224105105-1-GazeFix.csv
合并20210224105153-1-GazeFix.csv
合并20210224105227-1-GazeFix.csv
合并20210224105302-1-GazeFix.csv
合并20210224105336-1-GazeFix.csv
合并20210224113307-1-GazeFix.csv
合并20210224113355-1-GazeFix.csv
合并20210224113441-1-GazeFix.csv
合并20210224113516-1-GazeFix.csv
合并20210224113552-1-GazeFix.csv
合并20210224113631-0-GazeFix.csv
合并20210224113707-1-GazeFix.csv
合并20210224113746-1-GazeFix.csv
合并20210224145302-1-GazeFix.csv
合并20210224145408-1-GazeFix.csv
合并20210224145453-1-GazeFix.csv
合并20210224145539-1-GazeFix.csv
合并20210224145624-1-GazeFix.csv
合并20210224145701-1-GazeFix.csv
合并20210224145745-10-GazeFix.csv
合并20210224145824-1-GazeFix.csv
合并20210316075937-1-GazeFix.csv
合并20210316080052-1-GazeFix.csv
合并20210316080157-1-GazeFix.csv
合并20210316080235-1-GazeFix.csv
合并20210316080318-1-GazeFix.csv
合并20210316080403-1-GazeFix.csv
合并20210316080448-1-GazeFix.csv
合并20210316080542-1-GazeFix.csv
合并20210316080624-1-GazeFix.csv
合并20210316080716-1-GazeFix.csv
合并20210316080748-1-GazeFix.csv
合并20210316080857-1-GazeFix.csv
合并20210316080943-1-GazeFix.csv
合并20210316081019-1-GazeFix.csv
合并20210316081052-1-GazeFix.csv
合并20210316081124-1-GazeFix.csv
合并20210316081207-1-GazeFix.csv
合并20210316081249-1-GazeFix.csv
合并20210316081352-1-GazeFix.csv
合并20210316082700-1-GazeFix.csv
合并20210316082737-1-GazeFix.csv
合并20210316082814-1-GazeFix.csv
合并20210316082847-1-GazeFix.csv
合并20210316082921-1-GazeFix.csv
合并20210316083001-1-GazeFix.csv
合并20210316083040-1-GazeFix.csv
合并20210316083114-1-GazeFix.csv
合并20210316083147-1-GazeFix.csv
合并20210316083232-1-GazeFix.csv
合并20210316083307-1-GazeFix.csv
合并20210316083337-1-GazeFix.csv
合并20210316083526-1-GazeFix.csv
合并20210316084843-1-GazeFix.csv
合并20210316085010-1-GazeFix.csv
合并20210316085042-1-GazeFix.csv
合并20210316085134-1-GazeFix.csv
合并20210316085215-1-GazeFix.csv
合并20210316085254-1-GazeFix.csv
合并20210316085334-1-GazeFix.csv
合并20210316085401-1-GazeFix.csv
合并20210316085457-1-GazeFix.csv
合并20210316091924-1-GazeFix.csv
合并20210316092020-1-GazeFix.csv
合并20210316092056-4-GazeFix.csv
合并20210316092133-1-GazeFix.csv
合并20210316092209-1-GazeFix.csv
合并20210316092242-1-GazeFix.csv
合并20210316092332-1-GazeFix.csv
合并20210316092411-1-GazeFix.csv
合并20210316092658-1-GazeFix.csv
合并20210316092740-1-GazeFix.csv
合并20210316092814-1-GazeFix.csv
合并20210316092850-1-GazeFix.csv
合并20210316092928-1-GazeFix.csv
合并20210316093004-1-GazeFix.csv
合并20210316093048-1-GazeFix.csv
合并20210316093122-1-GazeFix.csv
合并20210316094048-1-GazeFix.csv
合并20210316094152-1-GazeFix.csv
合并20210316094236-1-GazeFix.csv
合并20210316094314-1-GazeFix.csv
合并20210316094347-1-GazeFix.csv
合并20210316094419-1-GazeFix.csv
合并20210316094508-1-GazeFix.csv
合并20210316094538-1-GazeFix.csv
合并20210316102536-1-GazeFix.csv
合并20210316102640-1-GazeFix.csv
合并20210316102717-1-GazeFix.csv
合并20210316102747-1-GazeFix.csv
合并20210316102830-1-GazeFix.csv
合并20210316102906-1-GazeFix.csv
合并20210316102944-1-GazeFix.csv
合并20210316103021-1-GazeFix.csv
合并20210316103058-1-GazeFix.csv
合并20210316103130-1-GazeFix.csv
合并20210316103158-1-GazeFix.csv
合并20210316112446-1-GazeFix.csv
合并20210316112527-1-GazeFix.csv
合并20210316112617-1-GazeFix.csv
合并20210316112655-1-GazeFix.csv
合并20210316112727-1-GazeFix.csv
合并20210316112802-1-GazeFix.csv
合并20210316112841-1-GazeFix.csv
合并20210316112915-1-GazeFix.csv
合并20210316112948-1-GazeFix.csv
合并20210316113038-1-GazeFix.csv
合并20210316113113-1-GazeFix.csv
合并结束,生成新文件:data.csv
import numpy as np
data = pd.read_csv(r'../对照组/total/data.csv')
print(data.columns)#获取列索引值
data1 = 0#获取列名为flow的数据作为新列的数据
data['experiment'] = data1 #将新列的名字设置为cha
data.to_csv(r"../对照组/total/data1.csv",mode = 'a',index =False)
#mode=a,以追加模式写入,header表示列名,默认为true,index表示行名,默认为true,再次写入不需要行名
print(data)
Index(['PosX', 'PosY', 'StartTimestamp', 'Length', 'Distance', 'Orientation',
       'Velocity'],
      dtype='object')
           PosX       PosY  StartTimestamp      Length  Distance  Orientation  \
0      205.3149   39.90735        728.1027   179.37280  16.83401    141.66500   
1      157.1331  -12.87659       1066.7960    45.90893  12.69407     68.80644   
2     1129.9380  371.15200       1511.3350    45.04490  20.13072     98.67117   
3     1176.4530  375.11730       3260.7620    45.10689  13.46727    109.52060   
4     1114.8270  378.29950       3555.7650   237.16930  40.38042     93.04492   
...         ...        ...             ...         ...       ...          ...   
4110   716.0582  439.63420       2572.2050   225.86060  27.55337    111.31510   
4111   721.6638  440.15170       3023.9260  2038.26900  38.53800     31.11211   
4112   721.6405  448.68320       5381.8660  2338.86700  42.88669     15.85577   
4113   690.1225  569.61980       7870.6670    79.28467  23.56705    170.96420   
4114   699.7314  514.75520       8088.3790   127.83810  29.25825    154.26420   

      Velocity  experiment  
0     0.105661           0  
1     0.031844           0  
2     0.011811           0  
3     0.053891           0  
4     0.454166           0  
...        ...         ...  
4110  0.121993           0  
4111  0.120555           0  
4112  0.286039           0  
4113  0.170248           0  
4114  1.273220           0  

[4115 rows x 8 columns]
import pandas as pd
import os
import glob
# 根据需要修改以下部分
path = os.path.abspath('../data/')
csv_list = glob.glob('../data/*.csv')
new_file_name = 'data.csv'  # 合并后的文件名
cols_new_name = [ 'PosX', 'PosY', 'StartTimestamp', 'Length', 'Distance', 'Orientation', 'Velocity', 'experiment']  # 汇总后的列名,根据需要修改
cols_num = [ 0, 1, 2, 3, 4, 5, 6, 7]  # 需要合并的列的索引,从0开始
csv_list
['../data\\data.csv', '../data\\data1.csv', '../data\\data2.csv']
file_allname = []  # 用于存储全部文件的名字
for filename in csv_list:
    t=filename.split("\\")[1]
    file_allname.append(t)  # 拼接.csv后缀,生成完整文件名

file_allname
['data.csv', 'data1.csv', 'data2.csv']
df = pd.DataFrame(cols_new_name).T
try:
    print('开始合并:')
    df.to_csv(path + '/' + new_file_name, encoding='gbk', header=False, index=False)
    for fn in file_allname:
        data = pd.read_csv(path + '/' + fn)
        print('合并' + fn)
        data = data.iloc[1:, cols_num]  # 跳过标题行
        data.to_csv(path + '/' + new_file_name, mode='a', encoding='gbk', header=False, index=False)
    print('合并结束,生成新文件:' + new_file_name)
except PermissionError as e:
    print('出现异常:' + str(type(e)) + '!\n文件已打开?请先关闭')
开始合并:
合并data.csv
合并data1.csv
合并data2.csv
合并结束,生成新文件:data.csv