简体   繁体   中英

Python for loop enumerate

I am reading multiple csv files and combine it in one csv file. The desired outcome of the combined data looks like the following:

0   4   6   8   10  12
1   2   5   4   2   1  
5   3   0   1   5   10
....

But in the following code, I intend the column to go from 0,4,6,8,10,12.

for indx, file in enumerate(files_File1):
    if file.endswith('csv'):  #reading csv filed in the designated folder
        filepath = os.path.join(folder_File1, file) #reading csv filed in the designated folder
        current = pd.read_csv(filepath, header=None) #reading csv filed in the designated folder
        if indx == 0:
            mydata_File1 = current.copy()
            mydata_File1.columns.values[1] = 4
            print(mydata_File1.columns.values)
        else:
            mydata_File1[2*indx+4] = current.iloc[:,1]
            print(mydata_File1.columns.values)

But instead, the outcome looks like this where the column goes from 0,2,4,6,8,10,12.

0   4   2   6   8   10  12
1   2       5   4   2   1  
5   3       0   1   5   10
....

I am not quite sure what causes the column named "2".

Any idea?

If you are really just trying to combine .csv files, no need for panda.

#! python3
import glob

folder_File1 = r"C:\Users\Public\Documents\Python\CombineCSVFiles"
csv_only = r"\*.csv"
files_File1 = glob.glob(f'{folder_File1}{csv_only}')
new_csv = f'{folder_File1}\\newcsv.csv'

lines = []
for file in files_File1:
    with open(file) as filein:
        if filein.name == new_csv:
            pass
        else:
            for line in filein:
                line = line.strip()  # or some other preprocessing
                lines.append(line)  # storing everything in memory!

with open(new_csv, 'w') as out_file:
    out_file.writelines(line + u'\n' for line in lines)

If there is some reason you need panda, then this will work. Your code references mydata_File1.columns.values which is the name of the columns, not the value in the columns. If this doesn't answer your question, then please provide more complete answer per @juanpa.arrivillaga comment.

#! python3
import os
import pandas as pd
import glob

folder_File1 = r"C:\Users\Public\Documents\Python\CombineCSVFiles"
csv_only = r"\*.csv"
files_File1 = glob.glob(f'{folder_File1}{csv_only}')
new_csv = f'{folder_File1}\\newcsv.csv'


mydata_File1 = []

for indx, file in enumerate(files_File1):
    if file == new_csv:
        pass
    else:
        current = pd.read_csv(file, header=None) #reading csv filed in the designated folder
        print (current)
        if indx == 0:
            mydata_File1 = current.copy()
            print(mydata_File1.values)
        else:
            pass
            mydata_File1 = mydata_File1.append(current, ignore_index=True)
            print(mydata_File1.values)

mydata_File1.to_csv(new_csv)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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