0

I am trying to create dataframe object from an existing CSV file in python but I am facing problems. I tried to import CSV file into python but I do not know whether I succeeded or not.

    >>> import os
    >>> userhome = os.path.expanduser('~')
    >>> csvfile= userhome + r'\Desktop\train.csv'
    >>> with open(csvfile, "r") as f:

After I wrote these statements it did not do anything.

So the First problem - Did I import CSV file to python? And if I did not how do I import?

After that how can I display data from CSV file in python?

I installed pandas

Python IDE 3.6.3 Shell

enter image description here

3
  • 1
    did you go through pandas. there is a function call read_csv which directly loads your csv to dataframe object. Commented Oct 21, 2017 at 19:11
  • I don't know if making the string a regex (using r'') will "fix" your use of backslashes. You normally need to use double backslash so you are escaping the escape character. ex: '\\Desktop\\train.csv'. I usually just get the path using unix path descriptions and handle it via os, like path = os.path.abspath('/Desktop/train.csv'). Commented Oct 21, 2017 at 19:23
  • Nothing is happening because your colon at the end is telling it that more commands are coming. Your with statement is saying "I want you to do something with f" but you never told it what. Commented Oct 21, 2017 at 19:29

2 Answers 2

1

Incase anyone is interested in using the python csv package to open csv files.

import os, csv
some_path = os.path.expanduser('~')
csv_file = some_path + '\path_to_file_from_home_dir\file_name.csv'
# Now let's read the file
with open (csv_file,'r+') as csvfile:
    csv_reader = csv.reader(csvfile, delimiter=' ')
    # Confirm that the code works by printing contents of the file 
    for row in csv_reader:
        print(', '.join(row))

You can read more about the csv reader module

to get its full capability

Sign up to request clarification or add additional context in comments.

Comments

0

Rather, use pandas to read the csv:

import pandas as pd
df = pd.read_csv(csvfile)

for additional options to read csv, refer to: pandas read csv

5 Comments

import pandas as ps df= ps.read_csv('train.csv') Traceback (most recent call last): File "<pyshell#77>", line 1, in <module> df= ps.read_csv('train.csv') File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/parsers.py", line 655, in parser_f return _read(filepath_or_buffer, kwds) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages/pandas/io/parsers.py", line 405, in _read parser = TextFileReader(filepath_or_buffer, **kwds) File "/Library/Frameworks/Python.framework/Versions/3.6/lib/pytho
I am getting multiple errors end of the trace it says FileNotFoundError: File b'train.csv' does not exist
@snowboard_maniac Sorry there was a typo. Look at my edit. Hope you installed pandas as well.
yes I have already installed it. After I executed it gives an error FileNotFoundError: File b'/Users/x\\Desktop\\train.csv' does not exist it says.Is it because of the two \\ sign? I can see the file on my desktop
@snowboard_maniac Give the full path: 'C:/Users/x/Desktop/train.csv'

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.