九道門丨pandas的使用技巧整理
Pandas是一個很棒的python庫,它
為我們提供了操作和分析資料的機會。
在本文中,我想談談大多數資料分析師或資料科學家在日常工作中每天使用的一些有用的pandas方法。
為了便於理解,我把這些方法分為了3個部分:
a。 資料理解
b。 資料清理
c。 資料分析與操作
資料理解
1. read_csv
()
df = pd。read_csv(‘。。\data\marketing_campaign。csv’ , sep=‘,’ )
pd。read_csv 方法可以幫助我們讀取資料集。如果我們的資料集除了逗號‘,’之外有不同的分隔符,例如製表符或分號,我們就可以更改sep引數sep = ‘ \t ’或sep = ‘ ; ’。使用pd。read_excel 和 pd。read_json 分別讀取 xlsx 和 json。
2. head
()
df。head() ## Returns first 5 rows
3. tail
()
df。tail() ## Returns last 5 rows of the dataset
4. info
()
df。info() 方法提供了列總數、行數、列名,每列中的變數數(非空值)、資料型別。因此,我們可以很容易地回答以下問題:
是否有缺失值?
資料型別是否有問題?
34 名客戶沒有其收入資訊
Dt_Customer 應為日期時間資料型別。
5. describe
()
df。describe() 方法預設返回數值資料的統計摘要。它包括計數、平均值、標準差、最小第一分位數、第二分位數(中位數)、第三分位數和最大值。
我們還可以使用 include 引數來包含其他列。
include = ‘ all ’ 另外返回
unique, top, freq
的特徵
unique = 每列中有多少個唯一值
top = 每列中哪個類出現最多
freq = 最頻繁出現的類的頻率。
df。describe(include = ‘all’)
6. isnull()
info 方法提供了許多有用的資訊,包括非空值。如果我們只想檢視資料集中的空值怎麼辦?
df。isnull()。sum() 方法返回空值。
7. duplicated()
duplicated()
方法負責
查詢資料集中的重複行。
如果行重複,則返回 False 或 True。
df[df。duplicated(keep=False)]
keep 引數有 3 個選項:
first、last 和 false。
first
:保留第一行
last
:保留最後一個
false:返回所有重複的行
資料清理
在分析資料之前,我們需要清理資料以進行準確的分析。
現在資料存在的問題有:
Income變數中缺少了一些值
Dt_Customer列的格式也是錯誤的
重複的行
讓我們看看如何用 pandas 來解決這些問題。
8. dropna()
df。dropna(inplace=True)
dropna() 方法刪除資料集中的所有行。
inplace 引數提供了是否永久刪除 null 值的選項。
注意:
如果沒有 inplace 引數,我們也可以賦值一個變數來建立新的資料幀,
例如
df1 = df。dropna()
mean = df。Income。mean() # average of Incomedf[‘Income’]。fillna(mean,inplace=True)
9. fillna()
fillna() 方法填充所需的空值。對於數值列,我們可以使用平均值或中位數,對於分類值,我們可以使用最常用的值(眾數)來插補值。
10. drop_duplicates()
預設情況下,drop_duplicates 方法會根據所有列刪除重複的行。
df。drop_duplicates(inplace=True) # inplace = True means we removed permanently。
11. to_datetime()
to_datetime() 函式的作用是將物件轉換為日期時間格式。
df[‘Dt_Customer’] = pd。to_datetime(df[‘Dt_Customer’])
如果我們想建立一個名為年,月或日的新列,我們還可以使用以下函式。
df[‘year_Customer’] = df[‘Dt_Customer’]。dt。yeardf[‘month_Customer’] = df[‘Dt_Customer’]。dt。month ## month of the customersdf[‘day_Customer’] = df[‘Dt_Customer’]。dt。dayofweek ## dayofweek as a number 0 = Monday 6 = Sundaydf[‘Dt_Customer’]。dt。strftime(‘%A’) # This shows full weekday name
資料分析和操作
12. value_counts()
df[‘Education’]。value_counts()
13. unique()
df[‘Education’]。unique()
14. nunique()
df[‘Education’]。nunique() Output = 5
15. sort_values()
如果我們想按收入對客戶進行排序,則可以使用sort_values。此方法允許我們
按任意列和任意順序對資料幀進行排序,即升序或降序。
df。sort_values(by=‘Income’, ascending=False)
16. query()
query() 方法允許按我們想要的條件過濾掉資料幀。
假設我們只想看到收入高於10萬的客戶。
df。query(‘Income > 100000’)
替代方式:
df[df[‘Income’] > 100000]
17. groupby()
group by method 通
過拆分物件來聚合我們的值。
df。groupby(‘Education’)[‘Income’]。mean()
替代方式:
df。groupby(‘Education’)。agg({‘Income’ : ‘mean’})
18. pivot_table()
pivot_table() 方法可以幫助我們建立有用的資料透視表。
我們需要使用4個引數作為輸入:資料、索引、列和值。
預設情況下,該方法使用均值作為聚合函式。
pd。pivot_table(data = df, values= ‘Income’, index = ‘Education’, columns = ‘Marital_Status’)
19. apply()
apply方法使我們能夠
使用內部的任何函式
。我們可以輕鬆地使用appliance方法進行任何計算。
假設我們要將數字轉換為客戶響應中的“已接受”和“未接受”字串。
df[‘Response’]。apply(lambda x : ‘Accepted’ if x == 1 else ‘Not Accepted’ )
20. replace()
df[‘Marital_Status’]。replace(to_replace=[‘Alone’,‘Divorced’,‘Widow’,‘YOLO’,‘Absurd’], value=‘Single’)
需要資料集的可以在評論區留言或者後臺私信我們。