Pandas を使った作業

PythonPythonBeginner
今すぐ練習

This tutorial is from open-source community. Access the source code

💡 このチュートリアルは英語版からAIによって翻訳されています。原文を確認するには、 ここをクリックしてください

はじめに

Pandas は、Python で開発された強力なデータ操作ツールです。柔軟性と使いやすさがあるため、データ分析やクリーニングで頻繁に使用されます。この実験では、Pandas を使ってデータの読み込み、データフレームの作成、データのアクセス、および単純な統計処理などの基本操作を行う方法を学びます。

VM のヒント

VM の起動が完了したら、左上隅をクリックして Notebook タブに切り替え、Jupyter Notebook を使って練習しましょう。

時々、Jupyter Notebook が読み込み完了するまで数秒待つ必要がある場合があります。Jupyter Notebook の制限により、操作の検証は自動化できません。

学習中に問題が発生した場合は、Labby にお問い合わせください。セッション後にフィードバックを提供してください。すぐに問題を解決いたします。


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL python(("Python")) -.-> python/BasicConceptsGroup(["Basic Concepts"]) pandas(("Pandas")) -.-> pandas/DataSelectionGroup(["Data Selection"]) python(("Python")) -.-> python/DataStructuresGroup(["Data Structures"]) python(("Python")) -.-> python/FunctionsGroup(["Functions"]) python(("Python")) -.-> python/ModulesandPackagesGroup(["Modules and Packages"]) python(("Python")) -.-> python/DataScienceandMachineLearningGroup(["Data Science and Machine Learning"]) python/BasicConceptsGroup -.-> python/comments("Comments") pandas/DataSelectionGroup -.-> pandas/select_columns("Select Columns") python/DataStructuresGroup -.-> python/lists("Lists") python/DataStructuresGroup -.-> python/tuples("Tuples") python/DataStructuresGroup -.-> python/dictionaries("Dictionaries") python/FunctionsGroup -.-> python/build_in_functions("Build-in Functions") python/ModulesandPackagesGroup -.-> python/importing_modules("Importing Modules") python/DataScienceandMachineLearningGroup -.-> python/numerical_computing("Numerical Computing") python/DataScienceandMachineLearningGroup -.-> python/data_analysis("Data Analysis") subgraph Lab Skills python/comments -.-> lab-65430{{"Pandas を使った作業"}} pandas/select_columns -.-> lab-65430{{"Pandas を使った作業"}} python/lists -.-> lab-65430{{"Pandas を使った作業"}} python/tuples -.-> lab-65430{{"Pandas を使った作業"}} python/dictionaries -.-> lab-65430{{"Pandas を使った作業"}} python/build_in_functions -.-> lab-65430{{"Pandas を使った作業"}} python/importing_modules -.-> lab-65430{{"Pandas を使った作業"}} python/numerical_computing -.-> lab-65430{{"Pandas を使った作業"}} python/data_analysis -.-> lab-65430{{"Pandas を使った作業"}} end

Pandas パッケージをインポートする

Pandas を使用する前に、インポートする必要があります。Pandas をエイリアス pd でインポートするのが一般的な方法です。

## Importing pandas package
import pandas as pd

データフレームを作成する

pandas のデータはデータフレームに格納されます。これは、列が異なる型である可能性のある 2 次元のラベル付きデータ構造です。

## Creating a DataFrame
df = pd.DataFrame(
    {
        "Name": [
            "Braund, Mr. Owen Harris",
            "Allen, Mr. William Henry",
            "Bonnell, Miss. Elizabeth",
        ],
        "Age": [22, 35, 58],
        "Sex": ["male", "male", "female"],
    }
)

列を選択する

特定の列のデータを操作したい場合は、列ラベルを使用して選択できます。結果は pandas の Series になります。

## Selecting the 'Age' column
df["Age"]

基本的な統計を行う

Pandas には、統計を行うための多くの機能が用意されています。たとえば、max() を使用して列の最大値を求めることができます。

## Finding the maximum age
df["Age"].max()

また、describe() を使用することで、DataFrame 内の数値データの概要をすばやく把握することができます。

## Describing the numerical data
df.describe()

まとめ

この実験では、Pandas パッケージをインポートし、データフレームを作成し、列を選択し、基本的な統計を行う方法を学びました。Pandas は、さまざまな種類のデータを扱える多用途のツールであり、データ分析と操作に最適な選択肢の 1 つです。