<jupyter_start><jupyter_text>Using Cloud Storage and BigQuery in Python<jupyter_code>import pandas as pd
from google.cloud import storage
from google.colab import auth
auth.authenticate_user()<jupyter_output><empty_output><jupyter_text>Download data from Cloud Storage<jupyter_code>def download_blob(project_id, bucket_name, source_blob_name, destination_file_name):
    """Downloads a blob from the bucket."""
    # project_id = "your-project-id"
    # bucket_name = "your-bucket-name"
    # source_blob_name = "storage-object-name"
    # destination_file_name = "local/path/to/file"

    storage_client = storage.Client(project_id)

    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(source_blob_name)
    blob.download_to_filename(destination_file_name)

    print(
        "Blob {} downloaded to {}.".format(
            source_blob_name, destination_file_name
        )
    )
download_blob(project_id='ba-780', bucket_name='ba-780', 
              source_blob_name='data/athlete_events.csv', 
              destination_file_name='athlete_events.csv')<jupyter_output>Blob data/athlete_events.csv downloaded to athlete_events.csv.<jupyter_text>Loading to pandas and processing<jupyter_code>athlete_events = pd.read_csv('athlete_events.csv')
athlete_events.head(3)
athlete_events_summer_2012 = athlete_events[athlete_events.Games == '2012 Summer']
athlete_events_summer_2012.shape
# write to csv
athlete_events_summer_2012.to_csv('athlete_events_summer_2012.csv')<jupyter_output><empty_output><jupyter_text>Uploading to Cloud Storage bucket<jupyter_code>def upload_blob(project_id, bucket_name, source_file_name, destination_blob_name):
    """Uploads a file to the bucket."""
    # project_id = "your-project-id"
    # bucket_name = "your-bucket-name"
    # source_file_name = "local/path/to/file"
    # destination_blob_name = "storage-object-name"

    storage_client = storage.Client(project_id)
    bucket = storage_client.bucket(bucket_name)
    blob = bucket.blob(destination_blob_name)

    blob.upload_from_filename(source_file_name)

    print(
        "File {} uploaded to {}.".format(
            source_file_name, destination_blob_name
        )
    )
upload_blob(project_id='ba-780', bucket_name='ba-780', 
            source_file_name='athlete_events_summer_2012.csv', 
            destination_blob_name='data/athlete_events_summer_2012.csv')<jupyter_output>File athlete_events_summer_2012.csv uploaded to data/athlete_events_summer_2012.csv.<jupyter_text>Download from BigQuery with Pandas<jupyter_code>customerChurn = pd.io.gbq.read_gbq(f'''
  SELECT * FROM `ba-780.examples.customerChurn`
''', project_id="ba-780")
customerChurn.head()
customer_churn_monthly_vs_payMethod = customerChurn.groupby('PaymentMethod')['MonthlyCharges'].mean().to_frame().reset_index()
customer_churn_monthly_vs_payMethod
customer_churn_monthly_vs_payMethod.to_gbq('temp.customer_churn_monthly_vs_payMethod',
                                           project_id='ba-780',
                                           if_exists='replace')<jupyter_output>1it [00:04,  4.82s/it]
{'fraction_non_alphanumeric': 0.1025558071821417, 'fraction_numerical': 0.021352313167259787, 'mean_word_length': 4.642335766423358, 'pattern_counts': {'":': 0, '<': 18, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 12, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '119411', 'license': 'MIT', 'n_tokens_mistral': 1069, 'n_tokens_neox': 993, 'n_words': 140, 'path': '01_Cloud_Storage_and_BigQuery_in_Python.ipynb', 'repo_name': 'maralinetorres/temp-780', 'size': '3091'}