Shakil Ahmed
Shakil Ahmed

Reputation: 13

Opening csv file in jupyter notebook

I tried to open a csv file in jupyter notebook, but it shows error message. And I didn't understand the error message. CSV file and jupyter notebook file is in the same directory. plz check the screenshot to see the error message

jupyter notebook code

csv file and jupyter notebook file is in same directory

Upvotes: 1

Views: 24596

Answers (3)

Piketar
Piketar

Reputation: 113

Use pandas for csv reading.

import pandas as pd 

df=pd.read_csv("AppleStore.csv")

You can used head/tail function to see the values. Use dtypes to see the types of all the values. You can check the documentation.

Upvotes: 1

Marco_CH
Marco_CH

Reputation: 3294

As others have written it's a bit difficult to understand what exactly is your problem.

But why don't you try something like:

with open("file.csv", "r") as table:
    for row in table:
        print(row)
        # do something

Or:

import pandas as pd

df = pd.read_csv("file.csv", sep=",")
# shows top 10 rows
df.head(10)
# do something

Upvotes: 3

Ari
Ari

Reputation: 6159

You can use the in-built csv package

import csv

with open('my_file.csv') as csv_file:
    csv_reader = csv.reader(csv_file, delimiter=',')
    for row in csv_reader:
       print(row)

This will print each row as an array of items representing each cell.

However, using Jupyter notebook you should use Pandas to nicely display the csv as a table.

import pandas as pd

df = pd.read_csv("test.csv")

# Displays top 5 rows
df.head(5)

# Displays whole table
df

Resources

The csv module implements classes to read and write tabular data in CSV format. It allows programmers to say, “write this data in the format preferred by Excel,” or “read data from this file which was generated by Excel,” without knowing the precise details of the CSV format used by Excel.

Read More CSV: https://docs.python.org/3/library/csv.html

pandas is an open source, BSD-licensed library providing high-performance, easy-to-use data structures and data analysis tools for the Python programming language.

Read More Pandas: https://pandas.pydata.org/pandas-docs/stable/getting_started/10min.html

Upvotes: 2

Related Questions