1

I want to create a for loop to name columns in multiple files. All of the files are .csv. After I name the files I want to append them. I think that I have a start on it below.

import csv
import pandas as pd


df0 = pd.read_csv('/home/jayaramdas/anaconda3/df/s110_s_b') 
df1 = pd.read_csv('/home/jayaramdas/anaconda3/df/s111_s_b') 
df2 = pd.read_csv('/home/jayaramdas/anaconda3/df/s112_s_b') 
df3 = pd.read_csv('/home/jayaramdas/anaconda3/df/s113_s_b') 
file_list = ['df0', 'df1', 'df2', 'df3']

for file in file_list:

    file.columns = ['date', 'bill_id', 'sponsor_id']

    df0 = df0.append(df1)
    df0 = df0.append(df2)
    df0 = df0.append(df3)


print (df0)

but I get the following error:

`enter code here`AttributeError                            Traceback (most recent call last)
<ipython-input-86-5d50a0488b24> in <module>()
      9 file_list = ['df0', 'df1', 'df2', 'df3']
     10 for file in file_list:
---> 11     file.columns = ['date', 'bill_id', 'sponsor_id']
     12 df0.columns = ['date', 'bill_id', 'sponsor_id']
     13 df0 = df0.append(df1)

AttributeError: 'str' object has no attribute 'columns'
Thomas K
  • 39,200
  • 7
  • 84
  • 86
Collective Action
  • 7,607
  • 15
  • 45
  • 60

1 Answers1

1

you may try this:

import pandas as pd
import glob

df = pd.concat((pd.read_csv(f, names=['date','bill_id','sponsor_id']) for f in glob.glob('/home/jayaramdas/anaconda3/df/s11?_s_b')))

PS if your CSV files are relatively small then you may want to read them into one string and then create data frame out of that string.

MaxU - stand with Ukraine
  • 205,989
  • 36
  • 386
  • 419