简体   繁体   中英

How to read csv file in Google Cloud Platform jupyter notebook

I am working on Jupyter notebook in google cloud platform AI notebook. Now I want to read.csv file in GCP which is stored locally in my laptop.

My approach:

df = pd.read_csv("C:\Users\Desktop\New Folder\Data.csv")

But its not working. How to read local file in GCP AI notebbok.

I don't think there is a direct way to do this, but here you have three alternatives:

a) Upload the file from the Jupyter UI:

1.Open the Jupyter UI.

2.In the left pane of the screen, at the top, below the menus, click the "Upload files" button.

3.Select the file from your local file system and click Open.

4.Once the file is available in the left pane of the screen, right-click the file and select "Copy Path" .

5.In your Notebook, type the following code, replacing test.csv with the path you just copied:

import pandas as pd    
df2 = pd.read_csv("test.csv")
print(df2)

b. Upload the file to the Notebooks instance's file system

1.Go to the Compute Engine screen in the GCP console.

2.SSH to your AI Platform Notebooks instance, using the SSH button.

3.In the new terminal window, click the gear icon and the "Upload File" option

4.Select the file from your local file system and click Open.

5.The file will be stored in $HOME/, optionally move it to the desired path.

6.In your Notebook, type the following code, replacing the path accordingly:

import pandas as pd
df = pd.read_csv("/path/to_file/test.csv")
print(df2)

c)Store the file in a GCS bucket.

1. Upload your file to GCS .

2.In your Notebook, type the following code, replacing the bucket and file names accordingly:

import pandas as pd
from google.cloud import storage
from io import BytesIO
client = storage.Client()
bucket_name = "your-bucket"
file_name = "your_file.csv"
bucket = client.get_bucket(bucket_name)
blob = bucket.get_blob(file_name)
content = blob.download_as_string()
df = pd.read_csv(BytesIO(content))
print(df)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM