Import file into python - python

I'm new to python and trying to learn a few exercises via colab. I want to import a CSV file that I saved to my desktop. Unfortunately, I keep getting a "cannot find file" error message. Not sure what I'm doing wrong.
Here's my code:
import os
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
mpg = pd.read_csv(r"C:\Users\micha\OneDrive\Desktop\mpg2018.csv.csv")
I tried to change csv.csv to csv.txt or leave as just .csv but nothing works. Any help would be great!

Foward slashes will work in this function.
mpg = pd.read_csv("C:/Users/micha/OneDrive/Desktop/filename.csv")
Since you've imported "os", you could also use path.join()
p = os.path.join("C:\\", "Users", "micha", "OneDrive", "Desktop", "a.csv")
mpg = pd.read_csv(p)
Also, that file format repetition within the name seems unnecessary. It may lead to more confusion.

You are using colab.research.google.com, which lives in its own cloud world and has no idea of what files are on your personal machine. However, if you
from google.colab import files
files.upload()
It will open a nice dialog box which will allow you to find the file in the usual way.

In files section you find Upload button click on it add your file in colab
now simply import pd.read_csv(path)
how you find path: left click on csv file then select copy path and paste in place of path in pd.read_csv()

Related

Python relative path or placeholder

I coded simple code to import merge filter calculate dataframe, but I can use only on my computer. How can I make it usable on any computer, via relative path or a placeholder?
Code for import dataframe is down, and code working only on my machine because file path is hard-coded, I need a solution how to code this to search .xlsx on desktop for example.
katalog_CJM = pd.read_excel(
r"C:\Users\adise\Desktop\Catalogue - 20.07.-09.08.2022 - EXAMPLE\10.Katalog_Katalog_CJM_20.07.2022-09.08.2022.xlsx",
sheet_name='Katalog')
Another code for export I need to save on any desktop or documents in windows:
Venera_merge.to_excel(
r"C:\Users\adise\Desktop\Catalogue - 20.07.-09.08.2022 - EXAMPLE\EXPORT PO DOBAVLJACIMA- PROGRAM\Venera.xlsx",
index=False)
Thanks to all
From following code you will get path same as %UserProfile% and then you can concatenate further path for desktop.
import os
os.environ['USERPROFILE']
Output:
C:\\User\\user_name

How to load a file in Pandas from a parent directory

I've not used Pandas in a while but wanted to load a JSON file.
I've traditionally had an overarching directory (on Mac) called DataAnalyis and store all the data I've collected in folders describing what they contain.
I then created a folder called IPythonnotebooks in which I kept my scripts.
Loading a file - let's call it 'dummy.json' was trivial. It's in a folder called dummy.
The code was simple:
import pandas as pd
df = pd.read_json('../dummy/dummy.json')
That doesn't work any more. What have I got wrong?
Update:
DataAnalysis
---dummy
----dummy.json
---IPythonnotebooks
----dummy.pynb
Apologies if this is not the correct way to present file structure. I start up the notebook file in the folder IPythonnotebooks
Sometimes JupyterLab (and maybe other 'notebooks') start with diff cwd than you might think.
import os
os.getcwd()
check if it matches your '../dummy/dummy.json'
or check if this works:
import os
fullpath = os.path.realpath("dummy.json")
df = pd.read_json(fullpath)

How do you know where a csv file is stored once you write a DataFrame onto disk?

import pandas as pd
hand_1=pd.DataFrame({
'Tables of 5':[5,10,15,20,25],
'Tables of 6':[6,12,18,24,30]})
hand_1.to_csv('Tables.csv')`
How do i find out where Tables.csv is stored?
Is this where python stores csv files by default and can this be changed?
It will be saved in your current working directory. If you would like to learn it, you can use the following code:
import os
current_directory = os.getcwd()
You can give a full path instead of tables.csv to store in another directory.

Problems with file path

I am trying to load a file from the following path:
path = 'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'
However, the file is not loading. If I move the file 1 level up and change the path to
path = 'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/S16309/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'
it loads.
The only difference is the S13893 folder.
I have the following code:
import nibabel as nib
import matplotlib.pyplot as plt
from scipy.misc import imsave as imsave
path = 'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'
im = nib.load(path).get_data()
print(im.shape)
any help would be great.
I tried using r in the beginning of the path, however it didn't work.
Also, I tried using \\?\ which again didn't work.
As a final resort, I found out that it was actually an issue with the path length and shortened the path by removing commonalities in the path for all files and it worked fine.
Thanks for all the help.
However, this is just a workaround I performed and the suggested edits/changes didn't work for me.
In Windows you should try with:
path = r'C:/Users/Aman/Alzheimer/test-network/ADNI1_Complete_1Yr_1.5T/ADNI/002_S_0685/MPR__GradWarp__B1_Correction__N3__Scaled/2006-07-06_10_36_49.0/ADNI_002_S_0685_MR_MPR__GradWarp__B1_Correction__N3__Scaled_Br_20070216235850690_S16309_I40683.nii'

Access data from the server in python

My teacher put up a large data in the school server and gave me a piece of code to open up the files. Below is my code:
import sys
sys.path.append("/Data/Data123/.local/lib/python2.7/site-packages")
from cloud.common import get_tile
from scipy.io import netcdf_file as copen
from scipy import interpolate
import matplotlib
from matplotlib import pyplot as pp
import matplotlib.cm as cm
import numpy
path = '/Data/a'
filenames = ['Hello.nc']
But when I run the code, it says there is "No such file or directory :'Hello.nc'
I am sure files are in that directory. So I want to ask, what did I do wrong?
Or am I not even collecting to the directory?
Thanks
It would be very difficult to answer this question without knowing what files are on the server and also looking at the code that actually connects, and attempts to retrieve the file; but I can provide you with this suggestion.
Use os.path instead of declaring a literal path string. For example:
Change:
path = '/Data/a'
To:
import os
path = os.path.join(r'/Data', 'a')
Then, when you concatenate path with a filename, use os.path.join again:
os.path.join(path, filenames[0]) # Modify to fit your filenames loop accordingly
The problem may be in the way you combine path with a filename. If this does not help, please add more code or double check the server to make sure the file is there.

Categories

Resources