Pandas 交叉表与 Pandas 数据透视表有何不同? [英] How is a Pandas crosstab different from a Pandas pivot_table?

查看:25
本文介绍了Pandas 交叉表与 Pandas 数据透视表有何不同?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

pandas.crosstab 和 Pandas 数据透视表似乎都提供了完全相同的功能.有什么不同吗?

Both the pandas.crosstab and the Pandas pivot table seem to provide the exact same functionality. Are there any differences?

推荐答案

两者的主要区别在于 pivot_table 期望你的输入数据已经是一个 DataFrame;您将 DataFrame 传递给 pivot_table 并通过将列名称作为字符串传递来指定 index/columns/values.使用 cross_tab,您不一定需要进入 DataFrame,因为您只需为 index/columns/传递类似数组的对象.

The main difference between the two is the pivot_table expects your input data to already be a DataFrame; you pass a DataFrame to pivot_table and specify the index/columns/values by passing the column names as strings. With cross_tab, you don't necessarily need to have a DataFrame going in, as you just pass array-like objects for index/columns/values.

查看relsource="nocrosstab 的代码,它本质上采用您传递的类似数组的对象,创建一个 DataFrame,然后根据需要调用 pivot_table.

通常,如果您已经有一个 DataFrame,请使用 pivot_table,这样您就没有再次创建相同 DataFrame 的额外开销.如果您从类似数组的对象开始并且只关心旋转数据,请使用 crosstab.在大多数情况下,我认为您决定使用哪个功能不会真正产生影响.

In general, use pivot_table if you already have a DataFrame, so you don't have the additional overhead of creating the same DataFrame again. If you're starting from array-like objects and are only concerned with the pivoted data, use crosstab. In most cases, I don't think it will really make a difference which function you decide to use.

这篇关于Pandas 交叉表与 Pandas 数据透视表有何不同?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆