How can I use a csv file that I have uploaded to GCS in a notebook?

Completed

Comments

2 comments

  • Avatar
    Mahesh Shenoy

    Sarath Botlagunta / Pradeepraj Chandrasekaran

    0
    Comment actions Permalink
  • Avatar
    Labinot Dvorani

    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())



    0
    Comment actions Permalink

Please sign in to leave a comment.