データ型とデータの結合

pandasで学ぶマーケティングキャンペーン分析

Jill Rosok

Data Scientist

一般的なデータ型

  • 文字列(object)
  • 数値(float、int)
  • 真偽値(True、False)
  • 日付
pandasで学ぶマーケティングキャンペーン分析

列のデータ型を確認

# Print a data type of a single column
print(marketing['converted'].dtype)
dtype('object')
pandasで学ぶマーケティングキャンペーン分析

列のデータ型を変更

# Change the data type of a column
marketing['converted'] = marketing['converted']\
                          .astype('bool')

print(marketing['converted'].dtype)
dtype('bool')
pandasで学ぶマーケティングキャンペーン分析

新しい真偽列の作成

marketing['is_house_ads'] = np.where(
    marketing['marketing_channel'] == 'House Ads', 
    True, False
)

print(marketing.is_house_ads.head(3))
0    True
1    False
2    True
Name: is_house_ads, dtype: bool
pandasで学ぶマーケティングキャンペーン分析

既存列への値のマッピング

channel_dict = {"House Ads": 1, "Instagram": 2, 
                "Facebook": 3, "Email": 4, "Push": 5}

marketing['channel_code'] = marketing['marketing_channel']\ .map(channel_dict) print(marketing['channel_code'].head(3))
0    1
1    1
2    1
Name: channel_code, dtype: int64
pandasで学ぶマーケティングキャンペーン分析

日付列

# Read date columns using parse_dates
marketing = pd.read_csv('marketing.csv', 
                        parse_dates=['date_served', 
                                     'date_subscribed', 
                                     'date_canceled'])

# Or
# Convert already existing column to datetime column
marketing['date_served'] = pd.to_datetime(
    marketing['date_served']
)
pandasで学ぶマーケティングキャンペーン分析

日付列

# Or convert each column individually
# Convert already existing column to datetime column
marketing['date_served'] = pd.to_datetime(
    marketing['date_served']
)
pandasで学ぶマーケティングキャンペーン分析

日付列

marketing['day_served'] = marketing['date_served']\
                       .dt.dayofweek
pandasで学ぶマーケティングキャンペーン分析

練習しましょう!

pandasで学ぶマーケティングキャンペーン分析

Preparing Video For Download...