簡體   English   中英

如果聚合多行數據,Pandas pivot_table aggfunc 將忽略類別

[英]Pandas pivot_table aggfunc ignores categories if more than one line of data is being aggregated

我正在嘗試使用 pandas.pivot_table 聚合 dataframe 並發現當在分類系列上聚合多行時它的行為不同。

此問題的代碼有助於解釋(盡管問題與我的不同)。

使用分類列設置 dataframe:

import pandas as pd

stations = ['Kings Cross Station', 'Newtown Station', 'Parramatta Station',
            'Town Hall Station', 'Central Station', 'Circular Quay Station', 
            'Martin Place Station', 'Museum Station', 'St James Station', 
            'Bondi Junction Station', 'North Sydney Station']

df1 = pd.DataFrame({'Station': ['Kings Cross Station', 'Newtown Station', 'Parramatta Station',
                                'Kings Cross Station', 'Newtown Station', 'Parramatta Station',
                                'Kings Cross Station', 'Newtown Station', 'Parramatta Station'],
                    'Date': pd.DatetimeIndex(['1/1/2017', '1/1/2017', '1/1/2017',
                                             '2/1/2017', '2/1/2017', '2/1/2017',
                                             '3/1/2017', '3/1/2017', '3/1/2017',]),
                    'Exit': range(0, 9)})

df1.Station = df1.Station.astype(pd.CategoricalDtype(stations, ordered=True))

如果我 pivot dataframe 與

df1.pivot_table(index = 'Date', columns= 'Station', values = 'Exit', 
                dropna=False, observed=False, aggfunc=len, fill_value=0)

我得到一個 dataframe ,其中所有類別的站點在 dataframe 中沒有數據作為填充 0 的列,這就是我想要的:

Station     Kings Cross Station  ...  North Sydney Station
Date                             ...                      
2017-01-01                    1  ...                     0
2017-02-01                    1  ...                     0
2017-03-01                    1  ...                     0
[3 rows x 11 columns]

但是,如果我添加一些具有重復值的行:

df2 = pd.DataFrame({'Station': ['Kings Cross Station', 'Newtown Station', 'Parramatta Station',
                                'Kings Cross Station', 'Newtown Station', 'Parramatta Station'],
                    'Date': pd.DatetimeIndex(['1/1/2017', '1/1/2017', '1/1/2017',
                                             '2/1/2017', '2/1/2017', '2/1/2017']),
                    'Exit': range(0, 6)})
df3 = pd.concat([df1, df2])

... 和 pivot

df3.pivot_table(index = 'Date', columns= 'Station', values = 'Exit', 
                dropna=False, observed=False, aggfunc=len, fill_value=0)

現在df3中未表示的站不會出現在 pivot 中:

Station     Kings Cross Station  Newtown Station  Parramatta Station
Date                                                                
2017-01-01                    2                2                   2
2017-02-01                    2                2                   2
2017-03-01                    1                1                   1

我可以通過迭代類別來添加缺失的類別,如果 pivot 表中沒有,則添加一列 0,但應該使用 pandas 來完成,當然?!

我希望這是清楚的,第一個問題! 謝謝

df1.dtypes
Station    category      
Date       datetime64[ns]
Exit       int64         
dtype: object

df2.dtypes
Station    object        
Date       datetime64[ns]
Exit       int64         
dtype: object

這是因為df2.Station還不是類別。 您必須應用與df1df2相同的轉換,pivot 才能工作。

在你的 concat 之前添加這一行應該可以解決問題:

df2.Station = df2.Station.astype(pd.CategoricalDtype(stations, ordered=True))

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM