How can I use a csv file that I have uploaded to GCS in a notebook?
CompletedI have uploaded a csv file using the file uploader feature. Can I get sample code on how I can load a csv file from GCS to a panda dataframe?
-
Hi Shawn,
I believe the below sample code would get you started.
Remember that authentication will need to be configured to access GCS resources.
from google.cloud import storage
import pandas as pd
# Set your GCS bucket and file paths
bucket_name = "your_bucket_name"
file_path = "path/to/your/file.csv"
# Initialize the GCS client
client = storage.Client()
# Get the GCS bucket
bucket = client.get_bucket(bucket_name)
# Get the blob (file) from the bucket
blob = bucket.blob(file_path)
# Download the blob's contents as a string
csv_content = blob.download_as_text()
# Create a pandas DataFrame from the CSV content
df = pd.read_csv(pd.compat.StringIO(csv_content))
# Now you can work with the pandas DataFrame (df)
print(df.head())
Please sign in to leave a comment.
Comments
2 comments