创建一个数据透视表,其中我的值是我的列的计数

时间:2018-03-12 05:36:06

标签: python pandas dataframe pivot-table

我有我的dtatframe,并希望显示数据,其中数据透视表中的vlaues只是字符串的数量,这是我在数据透视表中的列:

我的df示例:

trading_book    state
A               Traded Away
B               Dealer Reject
C               Dealer Reject
A               Dealer Reject
B               Dealer Reject
C               Dealer Reject
A               Dealer Reject
D               Dealer Reject
D               Dealer Reject
E               Dealer Reject

期望的结果:

    Traded Away Dealer Reject   Done
Book            
A          1           2          0
B          0           2          0
C          0           2          0
D          0           2          0
E          0           1          0

当我使用以下代码尝试此操作时:

Count_Row = df.shape[0] #gives number of row count
Count_Col = df.shape[1] #gives number of col count
df_Sample = df[['trading_book','state']].head(Count_Row-1)
display(df_Sample)

display(pd.pivot_table(
                   df_Sample, 
                   index=['trading_book'],
                   columns=['state'], 
                   values='state',
                   aggfunc='count'
              ))

我只收到显示的交易账簿

值和aggfunc参数需要做什么?

3 个答案:

答案 0 :(得分:2)

更正您的pivot_table代码:

v = df.pivot_table(
         index='trading_book', 
         columns='state', 
         aggfunc='size', 
         fill_value=0
)

只要指定values参数,就不需要指定aggfunc='size'参数。接下来,要获得准确的输出,您需要在列中reindex数据框:

v.reindex(columns=np.append(df.state.unique(), 'Done'), fill_value=0)

state         Traded Away  Dealer Reject  Done
trading_book                                  
A                       1              2     0
B                       0              2     0
C                       0              2     0
D                       0              2     0
E                       0              1     0

或者,在列表中指定所需的新列:

cols = ['Done', ...]
v.assign(**dict.fromkeys(cols, 0))

state         Dealer Reject  Traded Away  Done
trading_book                                  
A                         2            1     0
B                         2            0     0
C                         2            0     0
D                         2            0     0
E                         1            0     0

答案 1 :(得分:1)

您可以使用交叉表:

2018/03/12 11:42:45 [info] 10660#10372: *4506 client closed connection while waiting for request, client: 124.13.241.136, server: 0.0.0.0:16001
2018/03/12 11:44:40 [error] 10660#10372: *4508 lua entry thread aborted: runtime error: ./lua/fileupload.lua:9: attempt to concatenate field 'fileSaveListingPath' (a nil value)
stack traceback:
coroutine 0:
    ./lua/fileupload.lua: in function <./lua/fileupload.lua:1>, client: 124.13.241.136, server: db.hkltd.com, request: "POST /fileupload/ HTTP/1.1", host: "xxx.com:16003", referrer: "http://localhost:3000/catalogmanagement/8868"
2018/03/12 11:45:43 [info] 10660#10372: *4514 client closed connection while waiting for request, client: 124.13.241.136, server: 0.0.0.0:16003
2018/03/12 11:58:16 [error] 10660#10372: *4516 lua entry thread aborted: runtime error: ./lua/fileupload.lua:9: attempt to concatenate field 'fileSaveListingPath' (a nil value)
stack traceback:
coroutine 0:
    ./lua/fileupload.lua: in function <./lua/fileupload.lua:1>, client: 124.13.241.136, server: xxx.com, request: "POST /fileupload/ HTTP/1.1", host: "xxx:16003", referrer: "xxx.com:16003/catalogmanagement/8868"

答案 2 :(得分:1)

您可以在交叉表中的分类类型列中使用。通过使用类别。您告诉Pandas,即使它没有显示在此特定数据集中,也应将其视为一个选项。

            if (MainTree.SelectedItem is tblProject)
            {
                var s = (tblProject)MainTree.SelectedItem;
               _context.tblProjects.Remove(s);
            }

            if (MainTree.SelectedItem is tblLine)
            {
                var s = (tblLine)MainTree.SelectedItem;
                _context.tblLines.Remove(s);
            }

            if (MainTree.SelectedItem is tblDevice)
            {
                var s = (tblDevice)MainTree.SelectedItem;
                _context.tblDevices.Remove(s);
            }