close

[Solved] AttributeError: ‘list’ object has no attribute ‘to_csv’

Hello Guys, How are you all? Hope You all Are Fine. Today I get the following error AttributeError: ‘list’ object has no attribute ‘to_csv’ in python. So Here I am Explain to you all the possible solutions here.

Without wasting your time, Let’s start This Article to Solve This Error.

How AttributeError: ‘list’ object has no attribute ‘to_csv’ Error Occurs?

Today I get the following error AttributeError: ‘list’ object has no attribute ‘to_csv’ in python.

How To Solve AttributeError: ‘list’ object has no attribute ‘to_csv’Error ?

  1. How To Solve AttributeError: 'list' object has no attribute 'to_csv'Error ?

    To Solve AttributeError: 'list' object has no attribute 'to_csv'Error 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.

  2. AttributeError: 'list' object has no attribute 'to_csv'

    To Solve AttributeError: 'list' object has no attribute 'to_csv'Error 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.

Solution 1

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)

Solution 2

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)

Summery

It’s all About this issue. Hope all solution helped you a lot. Comment below Your thoughts and your queries. Also, Comment below which solution worked for you? Thank You.

Also, Read