Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/333.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如何计算表上的计数_Python_Pandas_Pivot Table - Fatal编程技术网

Python 如何计算表上的计数

Python 如何计算表上的计数,python,pandas,pivot-table,Python,Pandas,Pivot Table,我有这样的数据 import random import pandas as pd jobs = ['Agriculture', 'Crafts', 'Labor', 'Professional'] df = pd.DataFrame({ 'JobCategory':[random.choice(jobs) for i in range(300)], 'Region':[random.randint(1,5) for i in range(300)], 'Marita

我有这样的数据

import random
import pandas as pd

jobs = ['Agriculture', 'Crafts', 'Labor', 'Professional']

df = pd.DataFrame({
    'JobCategory':[random.choice(jobs) for i in range(300)],
    'Region':[random.randint(1,5) for i in range(300)],
    'MaritalStatus':[random.choice(['Not Married', 'Married']) for i in range(300)]
})
我想要一个简单的表格,显示每个地区的就业人数

print(pd.pivot_table(df,
           index='JobCategory',
           columns='Region',
           margins=True,
           aggfunc=len))
输出为

             MaritalStatus                               
Region                   1     2     3     4     5    All
JobCategory                                              
Agriculture           13.0  23.0  17.0  18.0   8.0   79.0
Crafts                16.0  13.0  18.0  19.0  14.0   80.0
Labor                 15.0  11.0  19.0  11.0  14.0   70.0
Professional          22.0  17.0  16.0   7.0   9.0   71.0
All                   66.0  64.0  70.0  55.0  45.0  300.0
我假设“MaritalStatus”显示在输出中,因为这是计算计数的列。如何让Pandas基于Region JobCategory计数进行计算并忽略dataframe中的无关列

在编辑中添加---

我正在寻找一个表与利润值要输出。我显示的表中的值是我想要的,但我不希望MaritalStatus是计数的值。如果该列中存在Nan,例如,将列定义更改为

'MaritalStatus':[random.choice(['Not Married', 'Married'])
                 for i in range(299)].append(np.NaN)
这是输出(有和没有
值='MaritalStatus',


您可以用0填充nan值,然后找到len,即

df = pd.DataFrame({
'JobCategory':[random.choice(jobs) for i in range(300)],
'Region':[random.randint(1,5) for i in range(300)],
'MaritalStatus':[random.choice(['Not Married', 'Married']) for i in range(299)].append(np.NaN)})

df = df.fillna(0)
print(pd.pivot_table(df,
       index='JobCategory',
       columns='Region',
       margins=True,
       values='MaritalStatus',
       aggfunc=len))
输出:

Region 1 2 3 4 5 All JobCategory Agriculture 19.0 17.0 13.0 20.0 9.0 78.0 Crafts 17.0 14.0 9.0 11.0 16.0 67.0 Labor 10.0 17.0 15.0 19.0 11.0 72.0 Professional 11.0 14.0 19.0 19.0 20.0 83.0 All 57.0 62.0 56.0 69.0 56.0 300.0 区域1 2 3 4 5所有 工作类别 农业19.017.013.020.0978.0 手工艺17.0 14.0 9.0 11.0 16.0 67.0 劳工10.0 17.0 15.0 19.0 11.0 72.0 专业11.014.019.019.020.083.0 全部57.0 62.0 56.0 69.0 56.0 300.0
您可以用0填充nan值,然后找到len,即

df = pd.DataFrame({
'JobCategory':[random.choice(jobs) for i in range(300)],
'Region':[random.randint(1,5) for i in range(300)],
'MaritalStatus':[random.choice(['Not Married', 'Married']) for i in range(299)].append(np.NaN)})

df = df.fillna(0)
print(pd.pivot_table(df,
       index='JobCategory',
       columns='Region',
       margins=True,
       values='MaritalStatus',
       aggfunc=len))
输出:

Region 1 2 3 4 5 All JobCategory Agriculture 19.0 17.0 13.0 20.0 9.0 78.0 Crafts 17.0 14.0 9.0 11.0 16.0 67.0 Labor 10.0 17.0 15.0 19.0 11.0 72.0 Professional 11.0 14.0 19.0 19.0 20.0 83.0 All 57.0 62.0 56.0 69.0 56.0 300.0 区域1 2 3 4 5所有 工作类别 农业19.017.013.020.0978.0 手工艺17.0 14.0 9.0 11.0 16.0 67.0 劳工10.0 17.0 15.0 19.0 11.0 72.0 专业11.014.019.019.020.083.0 全部57.0 62.0 56.0 69.0 56.0 300.0
len
聚合函数计算
MaritalStatus
值沿
JobCategory-区域的特定组合出现的次数。因此,您正在计算
JobCategory-Region
实例的数量,我猜这就是您所期望的。
len
聚合函数计算
MaritalStatus
值沿
JobCategory-Region
的特定组合出现的次数。因此,您正在计算
JobCategory-Region
实例的数量,我猜这就是您所期望的。

EDIT 我们可以为每个记录分配键值,并计算或调整该值的大小

df = pd.DataFrame({
'JobCategory':[random.choice(jobs) for i in range(300)],
'Region':[random.randint(1,5) for i in range(300)],
'MaritalStatus':[random.choice(['Not Married', 'Married']) for i in range(299)].append(np.NaN)})

print(pd.pivot_table(df.assign(key=1),
           index='JobCategory',
           columns='Region',
           margins=True,
           aggfunc=len,
           values='key'))
输出:

Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   16.0  14.0  13.0  16.0  16.0   75.0
Crafts        14.0   9.0  17.0  22.0  13.0   75.0
Labor         11.0  18.0  20.0  10.0  16.0   75.0
Professional  16.0  14.0  15.0  14.0  16.0   75.0
All           57.0  55.0  65.0  62.0  61.0  300.0
Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   10.0  18.0  10.0  15.0  19.0   72.0
Crafts        11.0  13.0  17.0  11.0  22.0   74.0
Labor         12.0  10.0  18.0  16.0  12.0   68.0
Professional  21.0  16.0  20.0  13.0  16.0   86.0
All           54.0  57.0  65.0  55.0  69.0  300.0
JobCategory   Region
Agriculture   1         10
              2         18
              3         10
              4         15
              5         19
Crafts        1         11
              2         13
              3         17
              4         11
              5         22
Labor         1         12
              2         10
              3         18
              4         16
              5         12
Professional  1         21
              2         16
              3         20
              4         13
              5         16
dtype: int64

您可以将MaritalStatus添加为
value
参数,这将消除列索引中的额外级别。使用aggfunc=
len
,选择什么作为值参数并不重要,它将为该聚合中的每一行返回1的计数

因此,请尝试:

print(pd.pivot_table(df,
           index='JobCategory',
           columns='Region',
           margins=True,
           aggfunc=len,
           values='MaritalStatus'))
输出:

Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   16.0  14.0  13.0  16.0  16.0   75.0
Crafts        14.0   9.0  17.0  22.0  13.0   75.0
Labor         11.0  18.0  20.0  10.0  16.0   75.0
Professional  16.0  14.0  15.0  14.0  16.0   75.0
All           57.0  55.0  65.0  62.0  61.0  300.0
Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   10.0  18.0  10.0  15.0  19.0   72.0
Crafts        11.0  13.0  17.0  11.0  22.0   74.0
Labor         12.0  10.0  18.0  16.0  12.0   68.0
Professional  21.0  16.0  20.0  13.0  16.0   86.0
All           54.0  57.0  65.0  55.0  69.0  300.0
JobCategory   Region
Agriculture   1         10
              2         18
              3         10
              4         15
              5         19
Crafts        1         11
              2         13
              3         17
              4         11
              5         22
Labor         1         12
              2         10
              3         18
              4         16
              5         12
Professional  1         21
              2         16
              3         20
              4         13
              5         16
dtype: int64
选择2 使用
groupby
size

df.groupby(['JobCategory','Region']).size()
输出:

Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   16.0  14.0  13.0  16.0  16.0   75.0
Crafts        14.0   9.0  17.0  22.0  13.0   75.0
Labor         11.0  18.0  20.0  10.0  16.0   75.0
Professional  16.0  14.0  15.0  14.0  16.0   75.0
All           57.0  55.0  65.0  62.0  61.0  300.0
Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   10.0  18.0  10.0  15.0  19.0   72.0
Crafts        11.0  13.0  17.0  11.0  22.0   74.0
Labor         12.0  10.0  18.0  16.0  12.0   68.0
Professional  21.0  16.0  20.0  13.0  16.0   86.0
All           54.0  57.0  65.0  55.0  69.0  300.0
JobCategory   Region
Agriculture   1         10
              2         18
              3         10
              4         15
              5         19
Crafts        1         11
              2         13
              3         17
              4         11
              5         22
Labor         1         12
              2         10
              3         18
              4         16
              5         12
Professional  1         21
              2         16
              3         20
              4         13
              5         16
dtype: int64
编辑 我们可以为每个记录分配键值,并计算或调整该值的大小

df = pd.DataFrame({
'JobCategory':[random.choice(jobs) for i in range(300)],
'Region':[random.randint(1,5) for i in range(300)],
'MaritalStatus':[random.choice(['Not Married', 'Married']) for i in range(299)].append(np.NaN)})

print(pd.pivot_table(df.assign(key=1),
           index='JobCategory',
           columns='Region',
           margins=True,
           aggfunc=len,
           values='key'))
输出:

Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   16.0  14.0  13.0  16.0  16.0   75.0
Crafts        14.0   9.0  17.0  22.0  13.0   75.0
Labor         11.0  18.0  20.0  10.0  16.0   75.0
Professional  16.0  14.0  15.0  14.0  16.0   75.0
All           57.0  55.0  65.0  62.0  61.0  300.0
Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   10.0  18.0  10.0  15.0  19.0   72.0
Crafts        11.0  13.0  17.0  11.0  22.0   74.0
Labor         12.0  10.0  18.0  16.0  12.0   68.0
Professional  21.0  16.0  20.0  13.0  16.0   86.0
All           54.0  57.0  65.0  55.0  69.0  300.0
JobCategory   Region
Agriculture   1         10
              2         18
              3         10
              4         15
              5         19
Crafts        1         11
              2         13
              3         17
              4         11
              5         22
Labor         1         12
              2         10
              3         18
              4         16
              5         12
Professional  1         21
              2         16
              3         20
              4         13
              5         16
dtype: int64

您可以将MaritalStatus添加为
value
参数,这将消除列索引中的额外级别。使用aggfunc=
len
,选择什么作为值参数并不重要,它将为该聚合中的每一行返回1的计数

因此,请尝试:

print(pd.pivot_table(df,
           index='JobCategory',
           columns='Region',
           margins=True,
           aggfunc=len,
           values='MaritalStatus'))
输出:

Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   16.0  14.0  13.0  16.0  16.0   75.0
Crafts        14.0   9.0  17.0  22.0  13.0   75.0
Labor         11.0  18.0  20.0  10.0  16.0   75.0
Professional  16.0  14.0  15.0  14.0  16.0   75.0
All           57.0  55.0  65.0  62.0  61.0  300.0
Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   10.0  18.0  10.0  15.0  19.0   72.0
Crafts        11.0  13.0  17.0  11.0  22.0   74.0
Labor         12.0  10.0  18.0  16.0  12.0   68.0
Professional  21.0  16.0  20.0  13.0  16.0   86.0
All           54.0  57.0  65.0  55.0  69.0  300.0
JobCategory   Region
Agriculture   1         10
              2         18
              3         10
              4         15
              5         19
Crafts        1         11
              2         13
              3         17
              4         11
              5         22
Labor         1         12
              2         10
              3         18
              4         16
              5         12
Professional  1         21
              2         16
              3         20
              4         13
              5         16
dtype: int64
选择2 使用
groupby
size

df.groupby(['JobCategory','Region']).size()
输出:

Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   16.0  14.0  13.0  16.0  16.0   75.0
Crafts        14.0   9.0  17.0  22.0  13.0   75.0
Labor         11.0  18.0  20.0  10.0  16.0   75.0
Professional  16.0  14.0  15.0  14.0  16.0   75.0
All           57.0  55.0  65.0  62.0  61.0  300.0
Region           1     2     3     4     5    All
JobCategory                                      
Agriculture   10.0  18.0  10.0  15.0  19.0   72.0
Crafts        11.0  13.0  17.0  11.0  22.0   74.0
Labor         12.0  10.0  18.0  16.0  12.0   68.0
Professional  21.0  16.0  20.0  13.0  16.0   86.0
All           54.0  57.0  65.0  55.0  69.0  300.0
JobCategory   Region
Agriculture   1         10
              2         18
              3         10
              4         15
              5         19
Crafts        1         11
              2         13
              3         17
              4         11
              5         22
Labor         1         12
              2         10
              3         18
              4         16
              5         12
Professional  1         21
              2         16
              3         20
              4         13
              5         16
dtype: int64

如果您将数据帧缩减为仅作为最终索引计数行一部分的列,则无需引用另一列即可工作

pd.pivot_table(testdata[['JobCategory', 'Region']],
               index='JobCategory',
               columns='Region',
               margins=True,
               aggfunc=len)

输出与问题中的相同,只是带“MaritialStatus”的行不存在。

如果将数据帧缩减为仅作为最终索引计数行一部分的列,则无需参考其他列即可工作

pd.pivot_table(testdata[['JobCategory', 'Region']],
               index='JobCategory',
               columns='Region',
               margins=True,
               aggfunc=len)

输出与问题中的相同,只是带“MaritialStatus”的行不存在。

在pivot_表示例中,如果我使用的额外列包含NaN,则所有边距值都是NaN。groupby没有为“All”提供值,必须将其转换为表格形式。请在问题中创建该示例。如果不想聚合NaN值,请查看大小和len与计数之间的差异。计数只计算非空值。@逼真度正常。请参见编辑,其中我为每个记录创建了一个值为1的“键”列,然后我计算该列以进行聚合。在pivot_表示例中,如果我使用的额外列包含NaN,则所有边距值均为NaN。groupby没有为“All”提供值,必须将其转换为表格形式。请在问题中创建该示例。如果不想聚合NaN值,请查看大小和len与计数之间的差异。计数只计算非空值。@逼真度正常。请参见编辑,其中我为每个记录创建了一个值为1的“键”列,然后我为聚合计算该列。不,我在计算每个组合中的MaritalStatus实例数。如果列中有NaN,这是有问题的。不,我正在计算每个组合中MaritalStatus实例的数量。如果列中有NaN,则会出现问题。不,我正在寻找一个具有边距值的表。您可以用一些值填充NaN,然后找到len。希望对您有所帮助否,我正在寻找一个有边距值的表格。您可以用一些值填充Nan,然后找到len。希望能有帮助