user14054515
user14054515

Reputation:

Getting ImportError when doing - from pandas import to_csv

I am making a GUI applet that needs to analyze data from many csv files (and also update them).

Right now all that I want is to read the data, update it, and then run pd.to_csv() on it.

I did this (first line of the code):

from pandas import read_csv, to_csv # because all that I want from pandas are these two things (for now)

Getting this error:

ImportError: cannot import name 'to_csv' from 'pandas' (C:\Users\<Your good username>\AppData\Local\Programs\Python\Python37-32\lib\site-packages\pandas\__init__.py)

Any advices?

Upvotes: 0

Views: 1460

Answers (2)

CK__
CK__

Reputation: 1311

to_csv is a part of DataFrame class. The below example should clear your doubts:

# importing pandas as pd 
import pandas as pd 

# list of name, location, pin 
nme = ["John", "Jacky", "Victor"] 
location = ["USA", "INDIA", "UK", "NL"] 
pin = [1120, 10, 770, 1990] 

# dictionary of lists 
df = pd.DataFrame({'name': nme, 'location': location, 'pin': pin})

# saving the dataframe 
df.to_csv('file2.csv', header=False, index=False) 

It will create a csv file.

Upvotes: 1

bilginyuksel
bilginyuksel

Reputation: 160

to_csv is a method of DataFrame class. So you can't import it like you import read_csv because read_csv is a function in pandas module but not to_csv.

Upvotes: 3

Related Questions