AttributeError: 'list' object has no attribute 'to_csv'
Question:
I’m trying to save my merged dataframe into a CSV file. As soon as the code hits on the “data.to_csv” line, it breaks. My code is as follows:
import pandas as pd
import numpy as np
import os,errno
import glob
print ("Path has been read successfully")
path1 = glob.glob('S:*Data*Files*Raw Data*CPU*Perf*YesterdayDataset*.xlsx')
print (path1)
path2 = glob.glob("S:*Data*Files*Raw Data*CPU*Perf*CPUPerf_201920.csv")
print ("Path has been read successfully")
print ("Action has been completed successfully")
data = []
for df in path1:
df = pd.read_excel(df)
data.append(df)
data.to_csv("H:\test1.csv", index = False)
def sremove():
try:
os.remove()
except OSError as e:
if e.errno != errno.ENOENT:
raise
The error I’m receiving after executing this code is:
AttributeError: ‘list’ object has no attribute ‘to_csv’
Any help on that will be greatly appreciated. Thank you
Answers:
You’re trying to use to_csv()
function on a list
and not on a dataframe. You have to merge your x dataframes to a single dataframe before you generate a csv from it.
Try something like this :
import pandas as pd
import numpy as np
import os,errno
import glob
print ("Path has been read successfully")
path1 = glob.glob('S:*Data*Files*Raw Data*CPU*Perf*YesterdayDataset*.xlsx')
print (path1)
path2 = glob.glob("S:*Data*Files*Raw Data*CPU*Perf*CPUPerf_201920.csv")
print ("Path has been read successfully")
print ("Action has been completed successfully")
data = []
for df in path1:
df = pd.read_excel(df)
data.append(df)
finaldf = pd.concat(data, axis=1, join='inner').sort_index()
finaldf.to_csv("H:\test1.csv", index = False)
def sremove():
try:
os.remove()
except OSError as e:
if e.errno != errno.ENOENT:
raise
your problem is that you have a list of data frames and you are calling to_csv on the whole list instead of the individual data frames. Two options here, if the list only has one data frame in it, use this code:
data[0].to_csv("H:\test1.csv", index = False)
if it has multiple data frames in it, do this:
for i in data:
i.to_csv("filename", index = False)
bear in mind that for the second option you will need to make your filenames dynamic to avoid overwriting.
EDIT:
Misread your question. You need to actually merge the data frames
df1 = pd.read_excel(path1)
df2 = pd.read_execl(path2)
df = df1.merge(df2, how = 'inner', on = 'JoinField')
df.to_csv("H:\test1.csv", index = False)
The problem is your data
object is a list
of the DataFrames. You can either convert the DataFrames individually, e.g. df.to_csv(...)
or merge them together and output as one file.
Try changing this part:
data = []
for df in path1:
df = pd.read_excel(df)
data.append(df)
data.to_csv("H:\test1.csv", index = False)
To this:
df = pd.concat(pd.read_excel(fl) for fl in path1)
df.to_csv("H:\test1.csv", index = False)
fwiw, I had a similar issue when attempting to create a copy of a dataframe.
df_copy = df[uids + ['id', 'name']].copy
vs
df_copy = df[uids + ['id', 'name']].copy()
Leaving the "()" off of the function call resulted in a list of dataframes and I received the same error as OP
I’m trying to save my merged dataframe into a CSV file. As soon as the code hits on the “data.to_csv” line, it breaks. My code is as follows:
import pandas as pd
import numpy as np
import os,errno
import glob
print ("Path has been read successfully")
path1 = glob.glob('S:*Data*Files*Raw Data*CPU*Perf*YesterdayDataset*.xlsx')
print (path1)
path2 = glob.glob("S:*Data*Files*Raw Data*CPU*Perf*CPUPerf_201920.csv")
print ("Path has been read successfully")
print ("Action has been completed successfully")
data = []
for df in path1:
df = pd.read_excel(df)
data.append(df)
data.to_csv("H:\test1.csv", index = False)
def sremove():
try:
os.remove()
except OSError as e:
if e.errno != errno.ENOENT:
raise
The error I’m receiving after executing this code is:
AttributeError: ‘list’ object has no attribute ‘to_csv’
Any help on that will be greatly appreciated. Thank you
You’re trying to use to_csv()
function on a list
and not on a dataframe. You have to merge your x dataframes to a single dataframe before you generate a csv from it.
Try something like this :
import pandas as pd
import numpy as np
import os,errno
import glob
print ("Path has been read successfully")
path1 = glob.glob('S:*Data*Files*Raw Data*CPU*Perf*YesterdayDataset*.xlsx')
print (path1)
path2 = glob.glob("S:*Data*Files*Raw Data*CPU*Perf*CPUPerf_201920.csv")
print ("Path has been read successfully")
print ("Action has been completed successfully")
data = []
for df in path1:
df = pd.read_excel(df)
data.append(df)
finaldf = pd.concat(data, axis=1, join='inner').sort_index()
finaldf.to_csv("H:\test1.csv", index = False)
def sremove():
try:
os.remove()
except OSError as e:
if e.errno != errno.ENOENT:
raise
your problem is that you have a list of data frames and you are calling to_csv on the whole list instead of the individual data frames. Two options here, if the list only has one data frame in it, use this code:
data[0].to_csv("H:\test1.csv", index = False)
if it has multiple data frames in it, do this:
for i in data:
i.to_csv("filename", index = False)
bear in mind that for the second option you will need to make your filenames dynamic to avoid overwriting.
EDIT:
Misread your question. You need to actually merge the data frames
df1 = pd.read_excel(path1)
df2 = pd.read_execl(path2)
df = df1.merge(df2, how = 'inner', on = 'JoinField')
df.to_csv("H:\test1.csv", index = False)
The problem is your data
object is a list
of the DataFrames. You can either convert the DataFrames individually, e.g. df.to_csv(...)
or merge them together and output as one file.
Try changing this part:
data = []
for df in path1:
df = pd.read_excel(df)
data.append(df)
data.to_csv("H:\test1.csv", index = False)
To this:
df = pd.concat(pd.read_excel(fl) for fl in path1)
df.to_csv("H:\test1.csv", index = False)
fwiw, I had a similar issue when attempting to create a copy of a dataframe.
df_copy = df[uids + ['id', 'name']].copy
vs
df_copy = df[uids + ['id', 'name']].copy()
Leaving the "()" off of the function call resulted in a list of dataframes and I received the same error as OP