Kiểu dữ liệu và gộp dữ liệu

Phân tích chiến dịch Marketing với pandas

Jill Rosok

Data Scientist

Các kiểu dữ liệu phổ biến

  • Chuỗi (object)
  • Số (float, integer)
  • Giá trị Boolean (True, False)
  • Ngày
Phân tích chiến dịch Marketing với pandas

Kiểu dữ liệu của một cột

# Print a data type of a single column
print(marketing['converted'].dtype)
dtype('object')
Phân tích chiến dịch Marketing với pandas

Đổi kiểu dữ liệu của cột

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

print(marketing['converted'].dtype)
dtype('bool')
Phân tích chiến dịch Marketing với pandas

Tạo cột Boolean mới

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
Phân tích chiến dịch Marketing với pandas

Ánh xạ giá trị vào cột hiện có

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
Phân tích chiến dịch Marketing với pandas

Cột ngày

# 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']
)
Phân tích chiến dịch Marketing với pandas

Cột ngày

# Or convert each column individually
# Convert already existing column to datetime column
marketing['date_served'] = pd.to_datetime(
    marketing['date_served']
)
Phân tích chiến dịch Marketing với pandas

Cột ngày

marketing['day_served'] = marketing['date_served']\
                       .dt.dayofweek
Phân tích chiến dịch Marketing với pandas

Ayo berlatih!

Phân tích chiến dịch Marketing với pandas

Preparing Video For Download...