ชนิดข้อมูลและการรวมข้อมูล

วิเคราะห์แคมเปญการตลาดด้วย pandas

Jill Rosok

Data Scientist

ชนิดข้อมูลทั่วไป

  • String (object)
  • ตัวเลข (float, integer)
  • ค่าบูลีน (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...