產生新特徵

Python 的探索性資料分析

George Boorman

Curriculum Manager, DataCamp

相關性

sns.heatmap(planes.corr(numeric_only=True), annot=True)
plt.show()

熱圖顯示 Price 與 Duration 的皮爾森相關係數為 0.54

Python 的探索性資料分析

檢視資料型別

print(planes.dtypes)
Airline                    object
Date_of_Journey    datetime64[ns]
Source                     object
Destination                object
Route                      object
Dep_Time           datetime64[ns]
Arrival_Time       datetime64[ns]
Duration                  float64
Total_Stops                object
Additional_Info            object
Price                     float64
dtype: object
Python 的探索性資料分析

中途停站數

print(planes["Total_Stops"].value_counts())
1 stop      4107
non-stop    2584
2 stops     1127
3 stops       29
4 stops        1
Name: Total_Stops, dtype: int64
Python 的探索性資料分析

清理中途停站數

planes["Total_Stops"] = planes["Total_Stops"].str.replace(" stops", "")

planes["Total_Stops"] = planes["Total_Stops"].str.replace(" stop", "")
planes["Total_Stops"] = planes["Total_Stops"].str.replace("non-stop", "0")
planes["Total_Stops"] = planes["Total_Stops"].astype(int)
Python 的探索性資料分析

相關性

sns.heatmap(planes.corr(numeric_only=True), annot=True)
plt.show()

熱圖顯示 Price 與 Total Stops 的相關為 0.62,Duration 與 Total Stops 的相關為 0.74

Python 的探索性資料分析

日期欄位

print(planes.dtypes)
Airline                    object
Date_of_Journey    datetime64[ns]
Source                     object
Destination                object
Route                      object
Dep_Time           datetime64[ns]
Arrival_Time       datetime64[ns]
Duration                  float64
Total_Stops                 int64
Additional_Info            object
Price                     float64
dtype: object
Python 的探索性資料分析

擷取月份與週幾

planes["month"] = planes["Date_of_Journey"].dt.month

planes["weekday"] = planes["Date_of_Journey"].dt.weekday
print(planes[["month", "weekday", "Date_of_Journey"]].head())
   month  weekday   Date_of_Journey
0      9        4        2019-09-06
1     12        3        2019-12-05
2      1        3        2019-01-03
3      6        0        2019-06-24
4     12        1        2019-12-03
Python 的探索性資料分析

起飛與到達時間

planes["Dep_Hour"] = planes["Dep_Time"].dt.hour
planes["Arrival_Hour"] = planes["Arrival_Time"].dt.hour
Python 的探索性資料分析

相關性

熱圖顯示日期時間屬性與價格幾乎無關

Python 的探索性資料分析

建立分類

print(planes["Price"].describe())
count     7848.000000
mean      9035.413609
std       4429.822081
min       1759.000000
25%       5228.000000
50%       8355.000000
75%      12373.000000
max      54826.000000
Name: Price, dtype: float64
範圍 票種
<= 5228 Economy
> 5228 <= 8355 Premium Economy
> 8335 <= 12373 Business Class
> 12373 First Class
Python 的探索性資料分析

描述統計量

twenty_fifth = planes["Price"].quantile(0.25)

median = planes["Price"].median()
seventy_fifth = planes["Price"].quantile(0.75)
maximum = planes["Price"].max()
Python 的探索性資料分析

標籤與分箱

labels = ["Economy", "Premium Economy", "Business Class", "First Class"]

bins = [0, twenty_fifth, median, seventy_fifth, maximum]
Python 的探索性資料分析

pd.cut()

呼叫 pd-dot-cut

planes["Price_Category"] = pd.cut(


Python 的探索性資料分析

pd.cut()

傳入資料

planes["Price_Category"] = pd.cut(planes["Price"],


Python 的探索性資料分析

pd.cut()

設定標籤

planes["Price_Category"] = pd.cut(planes["Price"],
                                  labels=labels,

Python 的探索性資料分析

pd.cut()

提供分箱

planes["Price_Category"] = pd.cut(planes["Price"],
                                  labels=labels,
                                  bins=bins)
Python 的探索性資料分析

票價分類

print(planes[["Price","Price_Category"]].head())
     Price   Price_Category
0  13882.0      First Class
1   6218.0  Premium Economy
2  13302.0      First Class
3   3873.0          Economy
4  11087.0   Business Class
Python 的探索性資料分析

各航空公司的票價分類

sns.countplot(data=planes, x="Airline", hue="Price_Category")
plt.show()
Python 的探索性資料分析

各航空公司的票價分類

計數圖顯示不同票價分類下各航空公司的航班數,Jet Airways 的頭等艙張數最多

Python 的探索性資料分析

一起來練習吧!

Python 的探索性資料分析

Preparing Video For Download...