10
8

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.

PandasのDataFrameをグラフ表示してCSVでダウンロードする

Last updated at Posted at 2018-05-09

試したこと

前回 で気象庁の「アメダス(表形式)」から取得したデータをグラフ表示&CSVダウンロードしてみる。

気温をグラフ表示

import matplotlib
import matplotlib.pyplot as plt

plt.style.use('ggplot')
df = tables[0].iloc[1:,:1]

df.columns=['temperature']
df=df.astype(float)
df.plot()
skitch.png

最初に試した時には TypeError: Empty 'DataFrame': no numeric data to plot となってしまった。

調べた所値が 整数ではなく小数点以下を含む 文字列として認識されている場合に発生するようなので、df=df.astype(float)を指定して回避した。

また、取得しているデータの先頭行に値の単位が文字列として入ってしまっている事が文字列として認識されている原因なので、下記のように予め単位も含めてヘッダ行にすればastypeの指定が不要だった。


tables = pd.read_html(url, flavor='bs4', match="時刻", header=[0,1], index_col=0)

@skotaroさんから頂いたコメントを反映

データをCSVでダウンロード

from google.colab import files

filename = 'sample.csv'

tables[0].to_csv(filename)
files.download(filename)

時刻,気温,降水量,風向,風速,日照時間,湿度,気圧
時,℃,mm,16方位,m/s,h,%,hPa
1,13.6,0.5,北,2.7,,99,1008.0
2,13.5,0.5,北北東,2.6,,99,1008.4
3,12.8,0.5,北北西,2.9,,100,1009.0
4,12.5,0.0,北北東,2.6,0.0,98,1009.5
5,12.5,0.0,北北東,2.7,0.0,97,1010.3
6,12.4,0.0,北北西,1.9,0.0,98,1011.2
・・・以下略

Colaboratoryのfilesでダウンロード出来る。
アップロードも同じように指定するとブラウザからアップロード出来る模様。

10
8
2

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
10
8

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?