【问题标题】:pandas dataframe : creating pivot_table on a data frame without giving values熊猫数据框:在数据框上创建数据透视表而不给出值
【发布时间】:2025-12-14 14:30:37
【问题描述】:

我有一个包含以下格式数据的文本文件

101.223.129.4 918801641445^0^paus
101.223.129.4 918801641445^0^german
101.223.129.4 918801641445^0^photo
101.223.129.4 918801641445^0^polish
101.223.129.4 918801641445^0^find
101.223.129.4 918801641445^0^extra
101.223.129.4 918801641445^0^access
101.223.129.4 918801641445^0^privat
101.223.129.4 918801641445^0^locat
101.223.129.4 918801641445^0^thank

我已使用以下代码在 pandas 中阅读此内容:

cols = ['msisdn','prob','desc']
txt_file = pd.read_csv('app_desc_fltrd.txt',sep="^",header=0,names=cols,low_memory=False)

现在我想使用

在这些数据上创建一个数据透视表
pivot_tbl = pd.pivot_table(data,index=['msisdn','prob'],columns=['desc'])

Traceback (most recent call last):
  File "transpose_txt_file.py", line 22, in <module>
    create_pivot(txt_file,cols)
  File "transpose_txt_file.py", line 15, in create_pivot
    pivot_tbl = pd.pivot_table(data,index=['msisdn','prob'],columns=['desc'])
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/util/decorators.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/util/decorators.py", line 88, in wrapper
    return func(*args, **kwargs)
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/tools/pivot.py", line 115, in pivot_table
    agged = grouped.agg(aggfunc)
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/core/groupby.py", line 676, in agg
    return self.aggregate(func, *args, **kwargs)
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/core/groupby.py", line 2615, in aggregate
    return getattr(self, arg)(*args, **kwargs)
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/core/groupby.py", line 691, in mean
    return self._cython_agg_general('mean')
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/core/groupby.py", line 2535, in _cython_agg_general
    new_items, new_blocks = self._cython_agg_blocks(how, numeric_only=numeric_only)
  File "/opt/anaconda2.2/lib/python2.7/site-packages/pandas/core/groupby.py", line 2585, in _cython_agg_blocks
    raise DataError('No numeric types to aggregate')
pandas.core.groupby.DataError: No numeric types to aggregate

只需稍作改动,我就能成功创建数据透视表。

pivot_tbl = pd.pivot_table(txt_file ,index=['msisdn'],columns=['desc'],values='desc')

但在这种情况下,我丢失了 msisdn 和 prob 字段的映射。

关于我如何创建数据透视表的任何建议,其中行中包含 ['msisdn','prob'],列中包含 ['desc'] 以及字段值中是否存在 desc 的指示变量将不胜感激。

基本上对于给定的 msisdn,概率永远不会改变。如果这能以任何方式提供帮助。

谢谢。

【问题讨论】:

  • 什么是data?这不是数据框txt_file
  • 没听懂你在问什么。但据我了解,是的 txt_file 是创建的数据框,原始数据存储在文件 app_desc_fltrd.txt 中。
  • 我的意思是pd.pivot_table(data,index=['msisdn'],columns=['desc'],value='desc')data 是什么??
  • 你能提供想要的输出吗?您希望如何呈现数据? Pandas 提供多种解决方案:transpose()groupby()stack(), wide_to_long(), melt(), pivot() 以及pivot_table()

标签: python python-2.7 pandas dataframe pivot-table


【解决方案1】:

您需要继续使用pivot_table - 您的代码中有一些拼写错误:

import pandas as pd

pd.pivot_table(txt_file, rows=['msisdn','prob'], cols='desc',aggfunc=len)

你也可以使用groupby:

txt_file.groupby(['prob','msisdn'])['desc'].value_counts().unstack()

【讨论】: