Search code examples
pythonpandascsvexport-to-csvopencsv

Need to pick 'second column' from multiple csv files and save all 'second columns' in one csv file


So I have 366 CSV files and I want to copy their second columns and write them into a new CSV file. Need a code for this job. I tried some codes available here but nothing works. please help.


Solution

  •     filenames = glob.glob(r'D:/CSV_FOLDER' + "/*.csv")
    
        new_df = pd.DataFrame()
    
        for filename in filenames:
            df = pd.read_csv(filename)
            second_column = df.iloc[:, 1]
            new_df[f'SECOND_COLUMN_{filename.upper()}'] = second_column
            del(df)
    
        new_df.to_csv('new_csv.csv', index=False)