偷偷摘套内射激情视频,久久精品99国产国产精,中文字幕无线乱码人妻,中文在线中文a,性爽19p

一行代碼制作數(shù)據(jù)分析交叉表,太便捷了

開發(fā) 后端
今天小編來介紹一下Pandas模塊中的另外一個函數(shù)corsstab(),我們可以通過調(diào)用該函數(shù)來制作交叉表,下面就來看看其中的主要流程和步驟吧。

在前文中我們了解到Pandas模塊中的pivot_table()函數(shù)可以用來制作數(shù)據(jù)透視表。

模塊導(dǎo)入和數(shù)據(jù)讀取

那我們按照慣例,首先導(dǎo)入模塊并且來讀取所要使用到的數(shù)據(jù)集,引用的依然是之前制作數(shù)據(jù)透視表的數(shù)據(jù)集

import pandas as pd
def load_data():
return pd.read_csv('coffee_sales.csv', parse_dates=['order_date'])

那這里小編是通過自定義一個函數(shù),然后通過調(diào)用該函數(shù)來讀取數(shù)據(jù),在實際工作當(dāng)中每個人都可以根據(jù)自己的喜好來操作:

df = load_data()
df.head()

output

牛刀小試

交叉表是用于統(tǒng)計分組頻率的特殊透視表。簡單來說,就是將兩個或者多個列中不重復(fù)的元素組成一個新的DataFrame,新數(shù)據(jù)的行和列交叉的部分值為其組合在原數(shù)據(jù)中的數(shù)量,我們先來看一個簡單的例子,代碼如下:

pd.crosstab(index = df['region'], columns = df['product_category'])

output

在行方向上代表的是不同的地區(qū),而在列方向上代表的則是不同的咖啡種類,出來的結(jié)果表示的是不同地區(qū)不同咖啡種類的匯總數(shù)據(jù),

df[(df["region"] == "Central")&(df["product_category"] == "Tea")].shape[0]

output

336

例如我們篩選出地區(qū)是中部地區(qū)并且品種是茶飲的數(shù)據(jù),出來的結(jié)果總共有336條數(shù)據(jù),和交叉表中的結(jié)果一致。

我們可以對列名以及行索引更換名字,通過調(diào)用rownames參數(shù)以及colnames參數(shù),代碼如下:

pd.crosstab(
index = df['region'],
columns = df['product_category'],
rownames=['US Region'],
colnames=['Product Category']
)

output

除了咖啡的品類之外,我們還想要知道到底不同品種的咖啡在批發(fā)和零售之間銷量的數(shù)據(jù),就可以這么來操作:

pd.crosstab(
index = df['region'],
columns = [df['product_category'], df['market']]
)

output

或者是

pd.crosstab(
index = df['region'],
columns = [df['product_category'], df['market']],
rownames=['US Region'],
colnames=['Product Category', 'Market']
)

output

輸出的DataFrame數(shù)據(jù)集當(dāng)中的列有兩層,最上面的是咖啡的種類,然后緊接著第二層的便是不同的市場,當(dāng)然我們也可以在行方向上添加多個層次的索引,代碼如下:

pd.crosstab(
index = [df['region'], df['market']],
columns = df['product_category']
)

output

進(jìn)階的操作

和pd.pivot_table()函數(shù)一樣,我們也可以通過調(diào)用當(dāng)中的margin參數(shù)來給整合出來的數(shù)據(jù)做一個加總,代碼如下:

pd.crosstab(index = df['region'],
columns = df['product_category'],
margins = True)

output

我們還能指定該列的列名。

pd.crosstab(
index = df['region'],
columns = df['product_category'],
margins = True,
margins_name = 'Subtotals'
)

output

另外還有參數(shù)normalize用來將所有值除以值的總和進(jìn)行歸一化。

pd.crosstab(index = df['region'],  
columns = df['product_category'],
normalize = True)

output

我們從美觀的角度出發(fā),想要保留兩位小數(shù),代碼如下:

pd.crosstab(
index = df['region'],
columns = df['product_category'],
normalize = True
).style.format('{:.2%}')

output

要是和之間的margin參數(shù)相結(jié)合來使用的話,將所有的結(jié)果匯總到一起等于100%,代碼如下:

pd.crosstab(
index = df['region'],
columns = df['product_category'],
margins = True,
normalize = True
).style.format('{:.2%}')

output

進(jìn)一步衍生

最后還有values以及aggfunc兩參數(shù),其中aggfunc參數(shù)具體指的是指定聚合函數(shù),例如平均數(shù)、求和以及中位數(shù)等統(tǒng)計方法,對value參數(shù)指定的連續(xù)性變量的列進(jìn)行計算。

df.info()

output

<class 'pandas.core.frame.DataFrame'>
RangeIndex: 4248 entries, 0 to 4247
Data columns (total 9 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 order_date 4248 non-null datetime64[ns]
1 market 4248 non-null object
2 region 4248 non-null object
3 product_category 4248 non-null object
4 product 4248 non-null object
5 cost 4248 non-null int64
6 inventory 4248 non-null int64
7 net_profit 4248 non-null int64
8 sales 4248 non-null int64
dtypes: datetime64[ns](1), int64(4), object(4)
memory usage: 298.8+ KB

當(dāng)前數(shù)據(jù)集中“market”、“region”、“product_category”、“product”四列是離散型變量,而有“cost”、“inventory”、“net_profit”、“sales”四列是連續(xù)性變量,分別代表的是成本、庫存、凈利潤以及銷量,其中我們想針對不同地區(qū)、不同咖啡種類的成本做一個平均值,那么代碼如下:

pd.crosstab(
index = df['region'],
columns = df['product_category'],
values = df['cost'],
aggfunc = 'mean'
)

output

要是我們想要對計算出來的結(jié)果保留兩位小數(shù),代碼如下:

pd.crosstab(
index = df['region'],
columns = df['product_category'],
values = df['cost'],
aggfunc = 'mean'
).round(2)

output

當(dāng)然要是針對存在缺失值的情況,我們也可以替換成其他值來處理,代碼如下:

pd.crosstab(
index = df['region'],
columns = df['product_category'],
values = df['cost'],
aggfunc = 'mean',
).fillna(0)

output

責(zé)任編輯:龐桂玉 來源: Python客棧
相關(guān)推薦

2022-06-17 09:21:53

Pandas代碼透視表

2021-04-29 22:38:04

Python數(shù)據(jù)庫SQL

2021-04-30 15:34:23

Python 開發(fā)編程語言

2024-12-27 09:12:12

C++17代碼元組

2016-12-02 08:53:18

Python一行代碼

2024-05-29 12:47:00

2020-02-14 12:26:55

Python愛心情人節(jié)

2021-02-24 14:30:59

JavaScript語言開發(fā)

2020-05-11 18:00:48

規(guī)范數(shù)據(jù)分析架構(gòu)

2022-02-24 10:40:14

Python代碼

2020-05-15 09:32:50

TB數(shù)據(jù)Elasticsear

2014-02-12 13:43:50

代碼并行任務(wù)

2022-04-09 09:11:33

Python

2017-04-05 11:10:23

Javascript代碼前端

2015-03-20 14:51:09

Testin云測

2025-04-21 10:43:21

2025-03-11 03:00:00

2020-08-21 09:23:22

Python開發(fā)工具

2021-11-11 11:27:55

大數(shù)據(jù)分析系統(tǒng)

2021-08-31 09:49:37

CPU執(zhí)行語言
點贊
收藏

51CTO技術(shù)棧公眾號