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

查看:210
本文介绍了 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/values传递类似数组的对象即可.

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.

查看源代码(对于crosstab),它实际上会接收您传递的类似数组的对象,创建一个DataFrame,然后视情况调用pivot_table.

Looking at the source code for crosstab, it essentially takes the array-like objects you pass, creates a DataFrame, then calls pivot_table as appropriate.

通常,如果您已经有一个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天全站免登陆