如何解决混乱的CSV复制

时间:2019-05-17 16:00:46

标签: python pandas csv export-to-csv

我正在尝试将一堆csv文件复制到1个大csv中。 所有3个文件都具有相同的列标题,但我根据文件名对其进行了更改。例如,文件arousal_a_103_happy.csv现在将在列标题的新csv中。

我的问题是 第一:它以非常奇怪的顺序复制文件,它不会翻转它,只是每一列都在它想要的位置。 第二:它不会将文件彼此相邻地复制,而是更像是一个斜坡。因此,如果第一个文件在P23完成,则新文件将从Q24开始。

这是代码:

def concatenate(path = "C:\\Users\\User\Desktop\\Work\\subject", outfile = "C:\\Users\\User\\Desktop\\Work\\subject\\concatenated.csv"):
    os.chdir(path)
    fileList=glob.glob("*happy.csv")
    dfList=[]
    print(fileList)
    i=1
    string = "subject" 
    for files in fileList:
        df = pd.read_csv("C:\\Users\\Desktop\\Work\\subject\\" + files, encoding ='CP1255') #Get's error because of the link!
        sub = files
        i+=1
        ColNames =[sub + " Level",  sub +" Description", sub+" Number",sub+" Onset_Date",sub+" Onset_Time",sub+" Offset_Date",sub+" Offset_Time",sub+" Duration_Date",sub+" Duration_Time",sub+" Arousal",sub+" Gaze",sub+" Movement"   , sub+" Vocalization", sub+" eyes covered", sub+" Mother\'s arrousal", sub+"    Transcript"]
        df.columns=ColNames
        dfList.append(df)

    concatDf = pd.concat(dfList, axis=0, ignore_index=True, verify_integrity=True)
    concatDf.to_csv(outfile, index=None)

1 个答案:

答案 0 :(得分:1)

# Fetching files
import csv
FileNames = []
path="C:\\Users\\User\Desktop\\Work\\subject\\"
os.chdir(path)
for counter, files in enumerate(glob.glob("*.csv")):
    FileNames.append(files)
print (FileNames)



# Merging all .csv from your folder 'subject'
pathout="C:\\Users\\User\Desktop\\Work\\subject\\"
for filenames in SortedFileNames:
    df = pd.read_csv(filenames,encoding='utf-8')
    saved_column = df.tweet
    saved_column.to_csv(pathout+"mixed.csv", mode='a')
print("File Created Sucessfully mixed.csv")