Home > Software engineering >  copy contents of a csv in a separate csv in pandas
copy contents of a csv in a separate csv in pandas

Time:03-30

I have multiple csv files with 2 columns each:

enter image description here

I want both these columns from all csv files to be copied into a single csv file as shown below: enter image description here

My code is as follows but I was only able to copy the last columns of each csv file (which would be the peak columns) into one file. I also need the time column of each file into the new one. I don't know how I could copy all the contents in one file. Is there a way to do it ? Please let me know.

for col in columns:
    filelist = [ 'Peakvalues_no_iso_'   col   '_100.csv',
                 'Peakvalues_no_iso_'   col   '_350.csv',]
   
    result = pd.DataFrame()
    
    for f in filelist:
        df = pd.read_csv(f)
        last_col = df.columns[-1]
        result = result.join(df[last_col], how='outer')
    result.reset_index(inplace=True)
    
    # Converting result DF into a csv file
    result.to_csv('Peak_noiso_'   col   '.csv', index=False)

CodePudding user response:

You can load files into a list and use pandas.concat to add all the column together. This will work for any number of files.

Here is the changed code:

for col in columns:
    filelist = [ 'Peakvalues_no_iso_'   col   '_100.csv',
                 'Peakvalues_no_iso_'   col   '_350.csv',]
   
    
    df_list = []
    
    for f in filelist:
        df = pd.read_csv(f)
        df_list.append(df)
        
    result = pd.concat(df_list, axis=1)
    result.reset_index(inplace=True)
    
    # Converting result DF into a csv file
    result.to_csv('Peak_noiso_'   col   '.csv', index=False)

CodePudding user response:

You can concat 2 dataframes using axis=1:

import pandas as pd

data = list(range(3))

# not loading csv - should make no difference as your time values do not coincide
df1 = pd.DataFrame( {"A": data, "B": [d 10 for d in data]})
df2 = pd.DataFrame( {"A": [d 20 for d in data], "B": [d 30 for d in data]})

result = pd.concat( [df1,df2], axis=1)

print(df1)
print(df2)
print(result)

Output:

# df 1
   A   B
0  0  10
1  1  11
2  2  12 

# df 2
    A   B
0  20  30
1  21  31
2  22  32 

# pd.concat( [df1,df2], axis=1)
   A   B   A   B
0  0  10  20  30
1  1  11  21  31
2  2  12  22  32 
  • Related