module ‘pandas’ has no attribute ‘to_csv’ ( Solved )


Pandas is the best python package for creating dataframe. You can manipulate dataframes using the pandas module. For example, you can read CSV using the read_csv() function as well as export data frame to CSV file using the to_csv() function. But sometimes while using this function you can get AttributeError. In this entire tutorial, you will know how to solve the issue of AttributeError module ‘pandas’ has no attribute ‘to_csv’ easily.

What is AttributeError ?

The AttributeError is an error you mostly get when you are using the wrong function. It means the function is not defined inside the package you are using. This type of error also comes when you are passing the wrong data type of the variable. For example, If I will pass the string variable instead of the list variable the append() method will throw the AttributeError.

Cause of AttributeError module ‘pandas’ has no attribute ‘to_csv’

The main or root cause of the error AttributeError module ‘pandas’ has no attribute ‘to_csv’ is that you are wrongly using the function to_csv() or you have not properly called the to_csv() function.

In most of the cases when you will get the module ‘pandas’ has no attribute ‘to_csv’ when you will wrongly importing and using the function.

You will get the same error when you will run the below lines of code.

import pandas as pd
data = {"col1":[1,2,3,4,5,6]}
df = pd.DataFrame(data=data)
pd.to_csv(df)

Output

module pandas has no attribute to_csv()
module pandas has no attribute to_csv()

Solution of module ‘pandas’ has no attribute ‘to_csv’

The solution to this attributeerror is very simple. You can clearly see in the above code I am invoking the function from pd.to_csv(). It is wrong. You have to use df.to_csv(“csv_file.csv”) instead of the pd.to_csv().

When you run the below lines of code then you will get the CSV file instead of the error.

import pandas as pd
data = {"col1":[1,2,3,4,5,6]}
df = pd.DataFrame(data=data)
df.to_csv("data.csv")
df = pd.read_csv("data.csv",index_col="col1")
print(df)

Output

Reading the data from the csv file
Reading the data from the csv file

Conclusion

Pandas python package has many inbuilt functions. You should always care while using the function. It can lead to attribute errors if you have used that function with the wrong variable type.

The above method will solve your error if you are getting the AttributeError module ‘pandas’ has no attribute ‘to_csv’.

Join our list

Subscribe to our mailing list and get interesting stuff and updates to your email inbox.

Thank you for signup. A Confirmation Email has been sent to your Email Address.

Something went wrong.





Source link