統一性

Pythonで学ぶデータクリーニング

Adel Nehme

VP of AI Curriculum, DataCamp

この章で学ぶこと

 

 

 

 

 

 

第3章 — 高度なデータ問題

Pythonで学ぶデータクリーニング

データの範囲制約

range_examples

Pythonで学ぶデータクリーニング

統一性

単位
Temperature 32°C 89.6°F でもある
Weight 70 Kg 11 st. でもある
Date 26-11-2019 26, November, 2019 でもある
Money 100$ 10763.90¥ でもある
Pythonで学ぶデータクリーニング

temperatures = pd.read_csv('temperature.csv')
temperatures.head()
       Date  Temperature
0  03.03.19         14.0
1  04.03.19         15.0
2  05.03.19         18.0
3  06.03.19         16.0
4  07.03.19         62.6
Pythonで学ぶデータクリーニング

temperatures = pd.read_csv('temperature.csv')
temperatures.head()
       Date  Temperature
0  03.03.19         14.0
1  04.03.19         15.0
2  05.03.19         18.0
3  06.03.19         16.0
4  07.03.19         62.6   <--
Pythonで学ぶデータクリーニング

# Import matplotlib
import matplotlib.pyplot as plt

# Create scatter plot plt.scatter(x = 'Date', y = 'Temperature', data = temperatures)
# Create title, xlabel and ylabel plt.title('Temperature in Celsius March 2019 - NYC') plt.xlabel('Dates') plt.ylabel('Temperature in Celsius')
# Show plot plt.show()
Pythonで学ぶデータクリーニング

Pythonで学ぶデータクリーニング

Pythonで学ぶデータクリーニング

気温データの処理

$$C = (F - 32) \times \frac{5}{9}$$

 

temp_fah = temperatures.loc[temperatures['Temperature'] > 40, 'Temperature']

temp_cels = (temp_fah - 32) * (5/9)
temperatures.loc[temperatures['Temperature'] > 40, 'Temperature'] = temp_cels
# Assert conversion is correct
assert temperatures['Temperature'].max() < 40
Pythonで学ぶデータクリーニング

日付データの処理

birthdays.head()
          Birthday First name Last name
0         27/27/19      Rowan     Nunez
1         03-29-19      Brynn      Yang
2  March 3rd, 2019     Sophia    Reilly
3         24-03-19     Deacon    Prince
4         06-03-19   Griffith      Neal
Pythonで学ぶデータクリーニング

日付データの処理

birthdays.head()

birthdays データセットの出力を示す表。1 行は月/日/年形式、別の行は英語での表記、最後の行は日が重複する明らかな誤り。

Pythonで学ぶデータクリーニング

Datetime の書式設定

datetime は日付の表現に有用

Date datetime format
25-12-2019 %d-%m-%Y
December 25th 2019 %c
12-25-2019 %m-%d-%Y
... ...

pandas.to_datetime()

  • 多くの形式を自動認識
  • 誤りや未知の形式では失敗することがある
Pythonで学ぶデータクリーニング

日付データの処理

# Converts to datetime - but won't work!
birthdays['Birthday'] = pd.to_datetime(birthdays['Birthday'])
ValueError: month must be in 1..12
# Will work!
birthdays['Birthday'] = pd.to_datetime(birthdays['Birthday'],
                                       # Return NA for rows where conversion failed
                                       errors = 'coerce')
Pythonで学ぶデータクリーニング

日付データの処理

birthdays.head()
    Birthday First name Last name
0        NaT      Rowan     Nunez
1 2019-03-29      Brynn      Yang
2 2019-03-03     Sophia    Reilly
3 2019-03-24     Deacon    Prince
4 2019-06-03   Griffith      Neal
Pythonで学ぶデータクリーニング

日付データの処理

birthdays['Birthday'] = birthdays['Birthday'].dt.strftime("%d-%m-%Y")
birthdays.head()
     Birthday First name Last name
0         NaT      Rowan     Nunez
1  29-03-2019      Brynn      Yang
2  03-03-2019     Sophia    Reilly
3  24-03-2019     Deacon    Prince
4  03-06-2019   Griffith      Neal
Pythonで学ぶデータクリーニング

あいまいな日付の処理

 

2019-03-08 は8月か3月か?

   

  • NA にして別途処理する
  • データ源の仕様から形式を推定する
  • DataFrame の前後関係から形式を推定する
Pythonで学ぶデータクリーニング

Passons à la pratique !

Pythonで学ぶデータクリーニング

Preparing Video For Download...