1

I am an R user and have recently been learning how to use Python!

In R, I normally import CSV files like this:

> getwd()
[1] "C:/Users/me/OneDrive/Documents"

my_file = read.csv("my_file.csv")

Now, I am trying to learn how to do this in Python.

I first tried this code and got the following error:

import pandas as pd

df = pandas.read_csv('C:\Users\me\OneDrive\Documents\my_file.csv')

File "<ipython-input-17-45a11fa3e8b1>", line 1
    df = pandas.read_csv('C:\Users\me\OneDrive\Documents\my_file.csv')
                         ^
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-3: truncated \UXXXXXXXX escape

I then tried this alternate method, but still got an error:

df = pandas.read_csv(r"C:\Users\me\OneDrive\Documents\my_file.csv")

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
<ipython-input-20-c0ac0d536b37> in <module>
----> 1 df = pandas.read_csv(r"C:\Users\me\OneDrive\Documents\my_file.csv")

NameError: name 'pandas' is not defined

Can someone please show me what I am doing wrong and how to fix this?

Thank you!

Note: I am using Jupyter Notebooks within Anaconda

stats_noob
  • 5,401
  • 4
  • 27
  • 83

2 Answers2

1

Regarding the second error, make sure pandas module is installed in your system. You can run this code snippet in the terminal to install the module.

pip install pandas -U

In python \somealphabet is represented as a Unicode character. What you can do is, you can either use \\somealphabet or replace \ with /

df = pd.read_csv('C:\\Users\\me\\OneDrive\\Documents\\my_file.csv')

df = pd.read_csv('C:/Users/me/OneDrive/Documents/my_file.csv')
  • Thank you! I am still getting this error: – stats_noob Nov 20 '22 at 04:42
  • --------------------------------------------------------------------------- NameError Traceback (most recent call last) in ----> 1 df = pandas.read_csv('C:/Users/me/OneDrive/Documents/Source_2.csv') NameError: name 'pandas' is not defined – stats_noob Nov 20 '22 at 04:42
  • You have imported your `pandas` module as `pd`. So every time you use pandas write it as `pd`. Check the updated answer. – vignesh kanakavalli Nov 20 '22 at 04:47
  • Thank you! I now get some new errors - there are too long so I posted them here: https://shrib.com/#Paxton4L0WEwQ . Can you please take a look at this? Thank you so much! – stats_noob Nov 20 '22 at 04:52
  • You can try one of these solutions. https://stackoverflow.com/questions/18171739/unicodedecodeerror-when-reading-csv-file-in-pandas-with-python – vignesh kanakavalli Nov 20 '22 at 04:55
  • unfortunately this is not working :( ... I will keep trying to figure this out! thank you so much! – stats_noob Nov 20 '22 at 05:32
  • I'd consider starting with pandas getting started guide - https://pandas.pydata.org/docs/getting_started/intro_tutorials/02_read_write.html - with provided Titanic dataset and preferably NOT reading it directly from OneDrive folder. – margusl Nov 20 '22 at 09:10
1
df = pd.read_csv(r'C:/Users/me/OneDrive/Documents/my_file.csv',  encoding='latin-1')

I fixed my own problem!

stats_noob
  • 5,401
  • 4
  • 27
  • 83