In dieser Anleitung fragen Sie Daten aus einem öffentlichen BigQuery-Dataset ab und untersuchen die Abfrageergebnisse in einem Notebook.
Lernziele
Erstellen Sie eine Abfrage in BigQuery und führen Sie sie aus.
Abfrageergebnisse in einem Notebook ansehen.
Kosten
In dieser Anleitung wird ein Dataset aus dem Google Cloud Public Datasets-Programm verwendet.
Google bezahlt die Speicherung dieser Datasets und bietet öffentlichen Zugriff auf die Daten. Für die Abfragen, die Sie für die Daten ausführen, fallen Kosten an. Weitere Informationen finden Sie unter BigQuery-Preise.
Hinweise
In the Google Cloud console, on the project selector page,
select or create a Google Cloud project.
Bei neuen Projekten ist BigQuery automatisch aktiviert.
Standardregion für Code-Assets festlegen
Wenn Sie zum ersten Mal ein Code-Asset erstellen, sollten Sie die Standardregion für Code-Assets festlegen. Sie können die Region für ein Code-Asset nicht mehr ändern, nachdem es erstellt wurde.
Für alle Code-Assets in BigQuery Studio wird dieselbe Standardregion verwendet.
So legen Sie die Standardregion für Code-Assets fest:
Sie können eine SQL-Abfrage ausführen und dann ein Notebook verwenden, um die Daten auszuwerten. Diese Vorgehensweise ist nützlich, wenn Sie die Daten in BigQuery ändern möchten, bevor Sie mit ihnen arbeiten, oder wenn Sie nur eine Teilmenge der Tabellenfelder benötigen.
Rufen Sie in der Google Cloud Console die Seite BigQuery auf.
Geben Sie in das Feld Suchbegriff eingebenbigquery-public-data ein.
Wenn das Projekt nicht angezeigt wird, geben Sie bigquery in das Suchfeld ein und klicken Sie dann auf In allen Projekten suchen, um den Suchstring mit den vorhandenen Projekten abzugleichen.
Wählen Sie bigquery-public-data > ml_datasets > penguins aus.
Klicken Sie für die Tabelle penguins auf more_vertAktionen anzeigen und dann auf Abfrage.
Fügen Sie der generierten Abfrage ein Sternchen (*) für die Feldauswahl hinzu, sodass sie wie im folgenden Beispiel aussieht:
Klicken Sie im Bereich Abfrageergebnisse auf Daten auswerten und dann auf Mit Python-Notebook auswerten.
Notebook für die Verwendung vorbereiten
Bereiten Sie das Notebook für die Verwendung vor, indem Sie eine Verbindung zu einer Laufzeit herstellen und Standardwerte für die Anwendung festlegen.
Klicken Sie im Codeblock Einrichtung auf play_circleZelle ausführen.
Öffentliche Daten durchsuchen
Um die penguins-Daten in einen BigQuery DataFrame zu laden und die Ergebnisse anzuzeigen, klicken Sie auf play_circleZelle ausführen im Codeblock im Abschnitt Ergebnismenge, die aus dem BigQuery-Job als DataFrame geladen wurde an.
Klicken Sie im Codeblock im Abschnitt Beschreibende Statistiken mit describe() anzeigen auf play_circleZelle ausführen, um beschreibende Messwerte für die Daten abzurufen.
Optional: Verwenden Sie andere Python-Funktionen oder -Pakete, um die Daten auszuwerten und zu analysieren.
Im folgenden Codebeispiel wird die Verwendung von bigframes.pandas zum Analysieren von Daten und von bigframes.ml zum Erstellen eines linearen Regressionsmodells aus Pinguin-Daten in einem BigQuery-DataFrame:
importbigframes.pandasasbpd# Load data from BigQueryquery_or_table="bigquery-public-data.ml_datasets.penguins"bq_df=bpd.read_gbq(query_or_table)# Inspect one of the columns (or series) of the DataFrame:bq_df["body_mass_g"]# Compute the mean of this series:average_body_mass=bq_df["body_mass_g"].mean()print(f"average_body_mass: {average_body_mass}")# Find the heaviest species using the groupby operation to calculate the# mean body_mass_g:(bq_df["body_mass_g"].groupby(by=bq_df["species"]).mean().sort_values(ascending=False).head(10))# Create the Linear Regression modelfrombigframes.ml.linear_modelimportLinearRegression# Filter down to the data we want to analyzeadelie_data=bq_df[bq_df.species=="Adelie Penguin (Pygoscelis adeliae)"]# Drop the columns we don't care aboutadelie_data=adelie_data.drop(columns=["species"])# Drop rows with nulls to get our training datatraining_data=adelie_data.dropna()# Pick feature columns and label columnX=training_data[["island","culmen_length_mm","culmen_depth_mm","flipper_length_mm","sex",]]y=training_data[["body_mass_g"]]model=LinearRegression(fit_intercept=False)model.fit(X,y)model.score(X,y)
Bereinigen
Damit Ihrem Google Cloud-Konto die in dieser Anleitung verwendeten Ressourcen nicht in Rechnung gestellt werden, löschen Sie entweder das Projekt, das die Ressourcen enthält, oder Sie behalten das Projekt und löschen die einzelnen Ressourcen.
Am einfachsten vermeiden Sie weitere Kosten, indem Sie das für diese Anleitung erstellte Google Cloud Projekt löschen.
In the Google Cloud console, go to the Manage resources page.
[[["Leicht verständlich","easyToUnderstand","thumb-up"],["Mein Problem wurde gelöst","solvedMyProblem","thumb-up"],["Sonstiges","otherUp","thumb-up"]],[["Schwer verständlich","hardToUnderstand","thumb-down"],["Informationen oder Beispielcode falsch","incorrectInformationOrSampleCode","thumb-down"],["Benötigte Informationen/Beispiele nicht gefunden","missingTheInformationSamplesINeed","thumb-down"],["Problem mit der Übersetzung","translationIssue","thumb-down"],["Sonstiges","otherDown","thumb-down"]],["Zuletzt aktualisiert: 2025-09-04 (UTC)."],[[["\u003cp\u003eYou can utilize Colab Enterprise notebooks within BigQuery to explore and analyze data from your query results.\u003c/p\u003e\n"],["\u003cp\u003eThis tutorial guides you through querying a public BigQuery dataset and then exploring its results within a notebook environment.\u003c/p\u003e\n"],["\u003cp\u003eBefore you can create and run notebooks, you must have a Google Cloud project with billing enabled, along with enabling the BigQuery API and BigQuery Studio, as well as having the necessary IAM roles.\u003c/p\u003e\n"],["\u003cp\u003eAfter running a SQL query, the data can be further explored by loading it into a BigQuery DataFrame, running descriptive statistics and implementing other python packages.\u003c/p\u003e\n"],["\u003cp\u003eWhen you are finished with this tutorial you can delete the project to avoid any further costs, or you can keep the project but delete the resources within it.\u003c/p\u003e\n"]]],[],null,["# Explore query results in notebooks\n\n*** ** * ** ***\n\nYou can explore BigQuery query results by using\n[Colab Enterprise notebooks](/colab/docs/introduction) in\nBigQuery.\n\nIn this tutorial, you query data from a\n[BigQuery public dataset](/bigquery/public-data)\nand explore the query results in a notebook.\n\nObjectives\n----------\n\n- Create and run a query in BigQuery.\n- Explore query results in a notebook.\n\nCosts\n-----\n\nThis tutorial uses a dataset available through the\n[Google Cloud Public Datasets Program](https://cloud.google.com/blog/products/data-analytics/big-data-analytics-in-the-cloud-with-free-public-datasets).\nGoogle pays for the storage of these datasets and provides public access to the\ndata. You incur charges for the queries that you perform on the data. For\nmore information, see\n[BigQuery pricing](/bigquery/pricing).\n\nBefore you begin\n----------------\n\n1. In the Google Cloud console, on the project selector page,\n select or create a Google Cloud project.\n\n | **Note**: If you don't plan to keep the resources that you create in this procedure, create a project instead of selecting an existing project. After you finish these steps, you can delete the project, removing all resources associated with the project.\n\n [Go to project selector](https://console.cloud.google.com/projectselector2/home/dashboard)\n2.\n [Verify that billing is enabled for your Google Cloud project](/billing/docs/how-to/verify-billing-enabled#confirm_billing_is_enabled_on_a_project).\n\n3.\n\n\n Enable the BigQuery API.\n\n\n [Enable the API](https://console.cloud.google.com/flows/enableapi?apiid=bigquery)\n\n For new projects, BigQuery is automatically enabled.\n\nSet the default region for code assets\n--------------------------------------\n\nIf this is the first time you are creating a code asset, you should set the\ndefault region for code assets. You can't change the region for a code asset\nafter it is created.\n| **Note:** If you create a code asset and choose a different default region than the one you have been using for code assets---for example, choosing `us-west1` when you have been using `us-central1`---then that code asset and all code assets you create afterwards use that new region by default. Existing code assets continue to use the region they were assigned when they were created.\n\nAll code assets in BigQuery Studio use the same default region.\nTo set the default region for code assets, follow these steps:\n\n1. Go to the **BigQuery** page.\n\n [Go to BigQuery](https://console.cloud.google.com/bigquery)\n2. In the **Explorer** pane, find the project in which you have enabled code\n assets.\n\n3. Click more_vert\n **View actions** next to the project, and then click\n **Change my default code region**.\n\n4. For **Region**, select the region that you want to use for code assets.\n\n5. Click **Select**.\n\nFor a list of supported regions, see [BigQuery Studio locations](/bigquery/docs/locations#bqstudio-loc). \n\n### Required permissions\n\nTo create and run notebooks, you need the following Identity and Access Management (IAM)\nroles:\n\n- [BigQuery User (`roles/bigquery.user`)](/bigquery/docs/access-control#bigquery.user)\n- [Notebook Runtime User (`roles/aiplatform.notebookRuntimeUser`)](/vertex-ai/docs/general/access-control#aiplatform.notebookRuntimeUser)\n- [Code Creator (`roles/dataform.codeCreator`)](/dataform/docs/access-control#dataform.codeCreator)\n\nOpen query results in a notebook\n--------------------------------\n\nYou can run a SQL query and then use a notebook to explore the data. This\napproach is useful if you want to modify the data in BigQuery\nbefore working with it, or if you need only a subset of the fields in the table.\n\n1. In the Google Cloud console, go to the **BigQuery** page.\n\n [Go to BigQuery](https://console.cloud.google.com/bigquery)\n2. In the **Type to search** field, enter `bigquery-public-data`.\n\n If the project is not shown, enter `bigquery` in the search field, and then\n click **Search to all projects** to match the search string with the\n existing projects.\n3. Select **bigquery-public-data \\\u003e ml_datasets \\\u003e penguins**.\n\n4. For the **penguins** table,\n click more_vert **View actions** ,\n and then click **Query**.\n\n5. Add an asterisk (`*`) for field selection to the generated query, so that\n it reads like the following example:\n\n ```googlesql\n SELECT * FROM `bigquery-public-data.ml_datasets.penguins` LIMIT 1000;\n ```\n6. Click play_circle **Run**.\n\n7. In the **Query results** section, click **Explore data** , and then click\n **Explore with Python notebook**.\n\nPrepare the notebook for use\n----------------------------\n\nPrepare the notebook for use by connecting to a runtime and setting application\ndefault values.\n\n1. In the notebook header, click **Connect** to [connect to the default runtime](/bigquery/docs/create-notebooks#connect_to_the_default_runtime).\n2. In the **Setup** code block, click play_circle **Run cell**.\n\nExplore the data\n----------------\n\n1. To load the **penguins** data into a [BigQuery DataFrame](/bigquery/docs/reference/bigquery-dataframes) and show the results, click play_circle **Run cell** in the code block in the **Result set loaded from BigQuery job as a DataFrame** section.\n2. To get descriptive metrics for the data, click play_circle **Run cell** in the code block in the **Show descriptive statistics using describe()** section.\n3. Optional: Use other Python functions or packages to explore and analyze the data.\n\nThe following code sample shows using\n[`bigframes.pandas`](/bigquery/docs/use-bigquery-dataframes)\nto analyze data, and [`bigframes.ml`](/bigquery/docs/use-bigquery-dataframes#ml-capabilities)\nto create a linear regression model from **penguins** data in a\nBigQuery DataFrame: \n\n import bigframes.pandas as bpd\n\n # Load data from BigQuery\n query_or_table = \"bigquery-public-data.ml_datasets.penguins\"\n bq_df = bpd.read_gbq(query_or_table)\n\n # Inspect one of the columns (or series) of the DataFrame:\n bq_df[\"body_mass_g\"]\n\n # Compute the mean of this series:\n average_body_mass = bq_df[\"body_mass_g\"].mean()\n print(f\"average_body_mass: {average_body_mass}\")\n\n # Find the heaviest species using the groupby operation to calculate the\n # mean body_mass_g:\n (\n bq_df[\"body_mass_g\"]\n .groupby(by=bq_df[\"species\"])\n .mean()\n .sort_values(ascending=False)\n .head(10)\n )\n\n # Create the Linear Regression model\n from bigframes.ml.linear_model import LinearRegression\n\n # Filter down to the data we want to analyze\n adelie_data = bq_df[bq_df.species == \"Adelie Penguin (Pygoscelis adeliae)\"]\n\n # Drop the columns we don't care about\n adelie_data = adelie_data.drop(columns=[\"species\"])\n\n # Drop rows with nulls to get our training data\n training_data = adelie_data.dropna()\n\n # Pick feature columns and label column\n X = training_data[\n [\n \"island\",\n \"culmen_length_mm\",\n \"culmen_depth_mm\",\n \"flipper_length_mm\",\n \"sex\",\n ]\n ]\n y = training_data[[\"body_mass_g\"]]\n\n model = LinearRegression(fit_intercept=False)\n model.fit(X, y)\n model.score(X, y)\n\nClean up\n--------\n\n\nTo avoid incurring charges to your Google Cloud account for the resources used in this\ntutorial, either delete the project that contains the resources, or keep the project and\ndelete the individual resources.\n\nThe easiest way to eliminate billing is to delete the Google Cloud project\nthat you created for this tutorial.\n\n| **Caution** : Deleting a project has the following effects:\n|\n| - **Everything in the project is deleted.** If you used an existing project for the tasks in this document, when you delete it, you also delete any other work you've done in the project.\n| - **Custom project IDs are lost.** When you created this project, you might have created a custom project ID that you want to use in the future. To preserve the URLs that use the project ID, such as an `appspot.com` URL, delete selected resources inside the project instead of deleting the whole project.\n|\n|\n| If you plan to explore multiple architectures, tutorials, or quickstarts, reusing projects\n| can help you avoid exceeding project quota limits.\n1. In the Google Cloud console, go to the **Manage resources** page.\n\n [Go to Manage resources](https://console.cloud.google.com/iam-admin/projects)\n2. In the project list, select the project that you want to delete, and then click **Delete**.\n3. In the dialog, type the project ID, and then click **Shut down** to delete the project.\n\nWhat's next\n-----------\n\n- Learn more about [creating notebooks in BigQuery](/bigquery/docs/create-notebooks).\n- Learn more about [exploring data with BigQuery DataFrames](/bigquery/docs/use-bigquery-dataframes)."]]