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'