0
0

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.

文字列を含むデータでも数値だけ取り出して手軽に可視化する

Posted at

実験データなどを可視化したいけどindexやcolumnsが文字列で扱いづらいと思った経験はありますよね。

例えばこんな風にある電場下での信号測定を温度を変えてプロットしたようなデータとかが該当します。

120V/mm 100V/mm
214K  1 5
216K  2 6
218K  3 7
220K  4 8

解決したいこと

グラフを作成するときにx座標になる配列が生データだと文字列なので数値だけ取り出したい。

解決方法

正規表現ライブラリを使って文字の部分を消してしまう。

# 正規表現操作ライブラリ
import re

# re.sub(正規表現パターン, 置換後文字列, 置換したい文字列)
x = [int(re.sub("\\D", "", temperature)) for temperature in data["120V/mm"].index]

y = data["120V/mm"]

re.sub("\D", "", temperature)の部分で十進数でない任意の文字列を空白””で置き換えて数字(文字列)だけ残し、int()によって整数値に変換する事によって温度の数値のみを取り出した配列を作成することができます。

print(x) #=>[214, 216, 218, 220]

以上のような簡単な方法で文字列を含むデータも可視化することができます。

import matplotlib.pyplot as plt
%matplotlib inline
plt.plot(x,y)

Unknown.png

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?