Getting ImportError when doing - from pandas import to_csv - python

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?

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.

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.

Related

How to display the csv file name which is read by pandas read_csv() function?

I want to display the csv file name which is read by pandas.read_csv() function. I tried the below code but I couldn't display the csv file name.
import pandas as pd
df=pd.read_csv("abc.csv")
print(df.info())
I want to display the "abc". Guide me for my situation. Thanks in advance.
The pandas.read_csv() method accepts a File object (actually any file-like object with a read() method).
And the File class has a name object that has the name of the opened file.
I see this code and situation as absolutely meaningless since you already know the file name beforehand, but for the sake of completeness, here you go:
import pandas as pd
csv_file = open("your_csv_filename.csv")
print(csv_file.name)
df = pd.read_csv(csv_file)
When you use pandas read_csv function, you get a dataframe that does not include the file name. So the solution is storing the name of the .csv in a variable, and then print it. You can check about pandas dataframe in pandas.DataFrame Documentation
import pandas as pd
name = "abc.csv"
df=pd.read_csv(name)
print(name.split(".")[0])
You can use something like this as read_csv does not save the file_name.
Using glob will give you the ability to put wildcards or regex for all the CSV files on that folder for reading.
import glob
data = {}
for filename in glob.glob("/path/of/the/csv/files/*.csv"):
data[filename.split("/")[-1].split(".")[0]] = pd.read_csv(filename)
for key, value in data.items():
print(key)
print(value.info())
print("\n\n")
filename.split("/")[-1].split('.')[0]
The above line may look complicated but it just split the file_name 2 times.

Problem when importing Excel File with Pandas

I'm new to python and was hoping someone could help me out.
I imported an excel file using pandas just to play around with. However when I try do any additional analysis or coding on the data it is only using the header row of the excel file.
Here's one of the codes I used:
import pandas as pd
df = pd.read_excel(r'C:\Users\at0789\Documents\Test File.xlsx')
data=list(df)
print(data)
Here's the output:
runfile('C:/Users/at0789/.spyder-py3/temp.py', wdir='C:/Users/at0789/.spyder-py3')
['Name', 'Number', 'Color', 'Date']
This is what my test file looks like:
you can pass only the string 'C:\Users\at0789\Documents\Test File.xlsx'
And you don't have to print the df, only call it, like that
import pandas as pd
df = pd.read_excel('C:\Users\at0789\Documents\Test File.xlsx')
df
import pandas as pd
df = pd.read_excel(r'C:\Users\at0789\Documents\Test File.xlsx')
df - data-frame
Data-frame have some many built-in function. With optimisation code with less line of code and high performance
One best feature is play example play with data as like sql query

Split multiple times?

So I'm currently transferring a txt file into a csv. It's mostly cleaned up, but even after splitting there are still empty columns between some of my data.
Below is my messy CSV file
And here is my current code:
Sat_File = '/Users'
output = '/Users2'
import csv
import matplotlib as plt
import pandas as pd
with open(Sat_File,'r') as sat:
with open(output,'w') as outfile:
if "2004" in line:
line=line.split(' ')
writer=csv.writer(outfile)
writer.writerow(line)
Basically, I'm just trying to eliminate those gaps between columns in the CSV picture I've provided. Thank you!
You can use python Pandas library to clear out the empty columns:
import pandas as pd
df = pd.read_csv('path_to_csv_file').dropna(axis=1, how='all')
df.to_csv('path_to_clean_csv_file')
Basically we:
Import the pandas library.
Read the csv file into a variable called df (stands for data frame).
Than we use the dropna function that allows to discard empty columns/rows. axis=1 means drop columns (0 means rows) and how='all' means drop columns all of the values in them are empty.
We save the clean data frame df to a new, clean csv file.
$$$ Pr0f!t $$$

Save an object from pandas_datareader into a csv file in Python 3.5

In order to save stock prices from yahoo into Python 3.5, I use the pandas module :
from pandas_datareader import data as dreader
symbols = ['AAPL','MRK']
pnls = {i:dreader.DataReader(i,'yahoo','2010-01-01','2016-09-01') for i in symbols}
It creates two "tables" (I don't know the name, sorry), one for each share (here 'AAPL' and 'MRK'). I want to save each table into a csv file but I don't know how. Anyone does?
Thanks,
Anthony
Just do this:
from pandas_datareader import data as dreader
symbols = ['AAPL','MRK']
for i in symbols:
dreader.DataReader(i,'yahoo','2010-01-01','2016-09-01').to_csv(i+'.csv')
It saves your data to two csv files.
It actually returns a pandas DataFrame. You can easily put a pandas DataFrame to csv file using the to_csv method.

how to save Python pandas data into excel file?

I am trying to load data from the web source and save it as a Excel file but not sure how to do it. What should I do? The original dataframe has different columns. Let's say that I am trying to save 'Open' column
import matplotlib.pyplot as plt
import pandas_datareader.data as web
import datetime
import pandas as pd
def ViewStockTrend(compcode):
start = datetime.datetime(2015,2,2)
end = datetime.datetime(2016,7,13)
stock = web.DataReader(compcode,'yahoo',start,end)
print(stock['Open'])
compcode = ['FDX','GOOGL','FB']
aa= ViewStockTrend(compcode)
Once you have made the pandas dataframe just use to_excel on the entire thing if you want:
aa.to_excel('output/filename.xlsx')
If stock is a pandas DataFrame, you need to construct a new Framefrom that column and output that one to excel:
df = pd.DataFrame(stock['Open'])
df.to_excel('path/to/your/file')

Categories