Basic Importing Excel Documents Into Python - python

I'm a new Python user and am simply trying to export an Excel (or CSV) file into Jupyter Notebook to play around with.
From google searching, the common code I see is something like the below:
import pandas as pd
from pandas import ExcelWriter
from pandas import ExcelFile
df = pd.read_excel('File.xlsx', sheetname='Sheet1')
print("Column headings:")
print(df.columns)
I tried this with a CSV file and got the below error message:
File "", line 5
df = pd.read_excel(C:\Users\dhauge1\Desktop\Python Workshop\fortune500.csv, sheetname=fortune500)
^ SyntaxError: invalid syntax
Please see above for error message. Is anyone able to help me understand what I'm doing wrong?

when reading a csv file use the comand pd.read_csv('filename')

Related

Pandas Not importing csv file correctly

Hey guys I've looked around a lot with importing csv files using pandas however even though my file path is correct I get thrown tons of errors
import pandas as pd
df = pd.read_csv(r"C:\Users\Liam\PycharmProjects\assignment1\pipeline-incidents-comprehensive-data.csv")
print(df)
all errors as seen here
I am very new to python (1 week) so i do realize this is a very simple problem so any assistance is greatly appreciated
import pandas as pd
path = "C:\\Users\\Liam\\PycharmProjects\\assignment1\\pipeline-incidents-comprehensive-data.csv"
df = pd.read_csv()
print(df)

Why isn't my file being found when converting excel spreadsheet to dataframe in pandas

I am doing something very simple, converting an excel spreadsheet to a pandas dataframe, but for some reason I keep getting this error: No such file or directory....
I have the file downloaded and saved to my computer and restarted my program, so I don't know what could be wrong. Any clue what's up?
Here is my code...
import pandas as pd
file_name ="file.xlsx"
dataframe = pd.read_excel(file_name)
print(dataframe)
You should have your "file.xlsx" in the same directory from where you call 'python' or specify full path to it (e.g. 'C:\file.xlsx' or '/home/user/file.xlsx')

What do I have to change that Jupyter shows columns?

I just want to import this csv file. It can read it but somehow it doesn't create columns. Does anyone know why?
This is my code:
import pandas as pd
songs_data = pd.read_csv('../datasets/spotify-top50.csv', encoding='latin-1')
songs_data.head(n=10)
Result that I see in Jupyter:
P.S.: I'm kinda new to Jupyter and programming, but after all I found it should work properly. I don't know why it doesn't do it.
To properly load a csv file you should specify some parameters. for example in you case you need to specify quotechar:
df = pd.read_csv('../datasets/spotify-top50.csv',quotechar='"',sep=',', encoding='latin-1')
df.head(10)
If you still have a problem you should have a look at your CSV file again and also pandas documentation, so that you can set parameters to match with your CSV file structure.

How to convert a csv file to a dataframe in Python 3.6 [duplicate]

This question already exists:
Reading CSV files in Python, using Jupyter Notebook through IntelliJ IDEA
Closed 4 years ago.
Im trying to tackle the Kaggle Titanic challenge. Bear with me, as Im fairly new to data science. I was previously struggling to get the following syntax to work: my previous question(Reading CSV files in Python 3.6, using IntelliJ IDEA)
Reading CSV files in Python, using Jupyter Notebook through IntelliJ IDEA
import numpy as np
import pandas as pd
from pandas import Series,Dataframe
titanic_df = pd.read_csv('train.csv')
titanic.head()
However, using the below code, I am able to open the file and read it/print its contents, but i need to convert the data to a dataframe so that it can be worked with. Any suggestions?
file_path = '/Volumes/LACIE SETUP/Data_Science/Data_Analysis_Viz_InPython/Example_Projects/train.csv'
with open(file_path) as train_fp:
for line in train_fp:
# print(line)
This above code was able to print out the data but when I tried passing
'file_path' to:
titanic_df = pd.read_csv('file_path.csv')
i received the same error as before. Not sure what Im doing wrong. I KNOW the file 'train.csv' exists in that location because 1) i put it there and 2) its contents can be printed when pointed to its location.
So what the heck am I doing wrong??? :/
read_csv will create a Pandas DataFrame. So, as long as your file path is right, this following code should work. Also, make sure to use the file_path variable and not the string "file_path.csv"
import pandas as pd
file_path = '/Volumes/LACIE SETUP/Data_Science/Data_Analysis_Viz_InPython/Example_Projects/train.csv'
titanic_df = pd.read_csv(file_path)
titanic_df.head()

Pandas excel reading buffer error (python 3)

I am having a problem reading an excel file from a download link using pandas. The excelString below loads correctly and looks like an excel file, but when trying to convert it to excel using pandas it says the file name is too long. Any assistance would be appreciated. This is a useful generic problem to solve for anyone accessing iShares index membership info.
import urllib
import pandas as pd
f = urllib.request.urlopen('https://www.ishares.com/us/239714/fund-download.dl')
excelString = f.read().decode('utf-8')
pd.ExcelFile(excelString)
The Error returned is OSError: [Errno 36] File name too long
Works fine for me using Python3 and pandas 0.16.2 - do you have the latest version?

Categories

Resources