0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Pandasの基本的な使い方

Posted at

はじめに

pandasとは、データ分析やデータ操作に用いられるPythonのライブラリの一つです。この記事ではpandasの基本的な使い方を簡単にまとめました。

Pandasのインストール

まずはインストールからです。
ターミナルを起動し、下記のコマンドを実行します。

install
pip install pandas

これでインストールが完了しました。
簡単ですね。
では、ちゃんとインストールされているか確認してみましょう。

Mac&Linux
python -c "import pandas as pd; print(pd.__version__)"
Windows
python -c "import pandas as pd; print(pd.__version__)"

上記を実行し、バージョンが表示されたらインストール完了しています。

Pandasの使い方

準備出来たので早速やってみましょう。
pandasを使用する為にimport pandas as pdでインポートをします。

import pandas as pd

pandasは様々な形式のファイルからデータを読み込む事が出来ます。試しにCSVファイルを読み込み、データの確認をしてみましょう。

# CSVファイルの読み込み
df = pl.read_csv("data.csv")

# データの確認
print(df.head())

特定の行を表示することも可能です。

# 最初の5行を表示
print(df.head())

# 最後の5行を表示
print(df.tail())

そして、特定の列のデータを抽出したり、データのフィルタリングをする事も出来ます。

# 'sample'列のデータを表示
print(df['sample'])

# 条件に基づいてデータを抽出
filtered_df = df[df['sample'] > 10]

加工したデータを新規保存するには下記を実行する事で保存出来ます。
また、Excelファイルとして保存する事も可能です。

# データを新しいCSVファイルに保存
df.to_csv('output.csv', index=False)

# Excelファイルに保存
df.to_excel('output.xlsx', index=False)

まとめ

少々駆け足になりましたが、以上がpandasの基本的な使い方になります。
pandasはExcelの様にデータ操作が出来るので表形式のデータ処理が簡単かつ膨大なデータ量にも対応出来る優れたライブラリです。
pandasを使いこなして、効率的なデータ分析を行っていきましょう。

0
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?