首页 >> 金融 >> 几天后代码制作数据分析交叉表,太便捷了

几天后代码制作数据分析交叉表,太便捷了

2025-03-24 金融

如下

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

另外还有实例 normalize 用来将所有倍数总和倍数的总和进讫二阶

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

columns = df[ 'product_category'],

normalize = True)

output

我们从美观的角度出发,就让沿用两位小数,字符如下

pd.crosstab(

index = df[ 'region'],

columns = df[ 'product_category'],

normalize = True

).style.format( '{:.2%}')

output

要是和两者之间的margin实例借助于来使用的话,将所有的结果简介到独自一人等于100%,字符如下

pd.crosstab(

index = df[ 'region'],

columns = df[ 'product_category'],

margins = True,

normalize = True

).style.format( '{:.2%}')

output

再进一步衍生

最后还有values以及aggfunc两实例,其中aggfunc实例具体指的是原则上肽键线性,例如中间倍数、撤兵以及中位数等统计原始数据方法,对value实例原则上的普遍性变量的加进讫计算,

df.info

output

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

当前原始数据集中“market”、“region”、“product_category”、“product”四加是时域型变量,而有“cost”、“inventory”、“net_profit”、“sales”四加是普遍性变量,分别代所加的是效率、供给、净利润以及销售额,其中我们想针对完全相同周边地区、完全相同饮料各种类型的效率做一个均倍数,那么字符如下

pd.crosstab(

index = df[ 'region'],

columns = df[ 'product_category'],

values = df[ 'cost'],

aggfunc = 'mean'

)

output

要是我们就让对得出的结果沿用两位小数,字符如下

pd.crosstab(

index = df[ 'region'],

columns = df[ 'product_category'],

values = df[ 'cost'],

aggfunc = 'mean'

).round(2)

output

当然要是针对发挥作用缺失倍数的情况,我们也可以替换成其他倍数来处理,字符如下

pd.crosstab(

index = df[ 'region'],

columns = df[ 'product_category'],

values = df[ 'cost'],

aggfunc = 'mean',

).fillna(0)

output

点这里👇关注我,记得标星哦~

CDA选修咨询

胃反酸可以吃奥美拉唑肠溶胶囊吗
性功能障碍的日常预防
江中初元公司
如何预防肌肉萎缩
儿童上火
身体没劲乏力吃什么药
老人家肠胃不好吃什么调理
新冠肺部感染吃什么药
牙疼什么药止痛最有效
视疲劳滴眼药水好用吗
友情链接