Why not login to Qiita and try out its useful features?

We'll deliver articles that match you.

You can read useful information later.

1
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?

More than 5 years have passed since last update.

【IBM Watson】気象観測データをPandasでデータ操作する

Last updated at Posted at 2019-03-28

概要

前回の記事の続きです。
今回はPandasでもう少し行列データを操作したいと思います。

Pandasでデータ操作

  • まず、Pandasでよく使う用語について記載しておきます。

    • Series:1次元配列のことです。
    • Data Frame:2次元配列(行列)データのことです。
    • Panel:3次元配列データのことです。
    • index:行ラベルのことです。
    • columns:列のことです。Programingをやられる方にとってはおなじみの用語ですね。
  • 用語について、軽く抑えたところで、進めていきたいと思います。

  • 前回は、気象庁観測データCSVを読み込んだところまで行きました。その続きでデータ操作を行います。

  • shapeで行列の数を求めることが出来ます。

  • columnsで列名を求めることが出来ます。

print('shape:', df_data_1.shape)
print('columns:', df_data_1.columns)

image.png

  • 上記で求めたcolumns を指定してデータを出力してみます。
df_data_1[['都道府県', '現在時刻(日)','現在時刻(時)', '現在時刻(分)', '現在値(mm)']]

image.png

  • 出力する条件を絞ってみましょう。都道府県が東京都のデータに絞ってみます。
  • 加工する前に、列名は日本語だと何かと難しいです。しかも、気象庁の天気データCSVは列名に半角括弧なども含まれているので、本格的にデータ操作するのであれば、英語に加工したり、半角括弧は変換するなりした方が良いですね。とりあえず、都道府県列で絞ってみました。
df_data_1[['都道府県', '現在時刻(日)','現在時刻(時)', '現在時刻(分)', '現在値(mm)']].query("都道府県=='東京都'")

image.png

  • Index(行番号)を指定して検索してみましょう
df_data_1[['都道府県', '現在時刻(日)','現在時刻(時)', '現在時刻(分)', '現在値(mm)']].loc[100:110]

image.png

  • 都道府県でgroup byして平均を求めてみます。group by した後にmean()で平均を求めています。ちなみにmaxを指定すると、最大値、minを指定すると最小値を求めることが出来ます。
df_data_1[['都道府県', '現在値(mm)']].groupby(['都道府県']).mean()

image.png

  • 要約統計量を取得(全カラム)
df_data_1.describe()

image.png

  • 都道府県、現在値(mm) で要約統計量を取得してみます。
df_data_1[['都道府県', '現在値(mm)']].describe()

image.png

  • ユニークの値とその出現回数をカウントしてみます。
df_data_1['都道府県'].value_counts()

image.png

  • 必要なカラムを絞って、'現在値(mm)'を降順でソートしてみましょう。sort_valuesでカラムを指定して、ascendingで昇順(True)、降順(False)を指定します。
df_data_1[['都道府県', '現在時刻(日)','現在時刻(時)', '現在時刻(分)', '現在値(mm)']].sort_values("現在値(mm)",ascending=False)

image.png

  • データ操作が簡単に行えて、面白いですね。きりが無いので紹介はこの辺りまでとしますが、興味がありましたら、この辺りを読んで色々と試してみてください。
1
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

Qiita Conference 2025 will be held!: 4/23(wed) - 4/25(Fri)

Qiita Conference is the largest tech conference in Qiita!

Keynote Speaker

ymrl、Masanobu Naruse, Takeshi Kano, Junichi Ito, uhyo, Hiroshi Tokumaru, MinoDriven, Minorun, Hiroyuki Sakuraba, tenntenn, drken, konifar

View event details
1
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?