用法:
pandas.crosstab(index, columns, values=None, rownames=None, colnames=None, aggfunc=None, margins=False, margins_name='All', dropna=True, normalize=False)
計算兩個(或更多)因子的簡單交叉表。默認情況下,除非傳遞值數組和聚合函數,否則會計算因子的頻率表。
- index:array-like、係列或數組/係列列表
行中分組依據的值。
- columns:array-like、係列或數組/係列列表
列中分組依據的值。
- values:array-like,可選
根據因子聚合的值數組。需要指定
aggfunc
。- rownames:序列,默認無
如果通過,則必須匹配通過的行數組數。
- colnames:序列,默認無
如果通過,則必須匹配通過的列數組的數量。
- aggfunc:函數,可選
如果指定,還需要指定
values
。- margins:布爾值,默認為 False
添加行/列邊距(小計)。
- margins_name:str,默認“全部”
當邊距為 True 時將包含總計的行/列的名稱。
- dropna:布爾值,默認為真
不要包括其條目全部為 NaN 的列。
- normalize:bool,{‘all’, ‘index’, ‘columns’} 或 {0,1},默認為 False
通過將所有值除以值的總和進行歸一化。
如果通過 ‘all’ 或
True
,將對所有值進行標準化。如果通過 ‘index’ 將在每一行上標準化。
如果通過 ‘columns’ 將在每一列上標準化。
如果邊距是
True
,也會標準化邊距值。
- DataFrame
數據的交叉表。
參數:
返回:
注意:
除非指定cross-tabulation 的行名或列名,否則傳遞的任何係列都將使用其名稱屬性。
任何傳遞的包含分類數據的輸入都將在cross-tabulation 中包含其所有類別,即使實際數據不包含特定類別的任何實例。
如果沒有重疊的索引,則將返回一個空的 DataFrame。
有關更多示例,請參閱用戶指南。
例子:
>>> a = np.array(["foo", "foo", "foo", "foo", "bar", "bar", ... "bar", "bar", "foo", "foo", "foo"], dtype=object) >>> b = np.array(["one", "one", "one", "two", "one", "one", ... "one", "two", "two", "two", "one"], dtype=object) >>> c = np.array(["dull", "dull", "shiny", "dull", "dull", "shiny", ... "shiny", "dull", "shiny", "shiny", "shiny"], ... dtype=object) >>> pd.crosstab(a, [b, c], rownames=['a'], colnames=['b', 'c']) b one two c dull shiny dull shiny a bar 1 2 1 0 foo 2 2 1 2
這裏 ‘c’ and ‘f’ 沒有在數據中表示,也不會顯示在輸出中,因為 dropna 默認為 True。設置 dropna=False 以保留沒有數據的類別。
>>> foo = pd.Categorical(['a', 'b'], categories=['a', 'b', 'c']) >>> bar = pd.Categorical(['d', 'e'], categories=['d', 'e', 'f']) >>> pd.crosstab(foo, bar) col_0 d e row_0 a 1 0 b 0 1 >>> pd.crosstab(foo, bar, dropna=False) col_0 d e f row_0 a 1 0 0 b 0 1 0 c 0 0 0
相關用法
- Python pandas.crosstab()用法及代碼示例
- Python pandas.core.resample.Resampler.nearest用法及代碼示例
- Python pandas.core.groupby.GroupBy.nth用法及代碼示例
- Python pandas.core.groupby.SeriesGroupBy.unique用法及代碼示例
- Python pandas.core.window.rolling.Window.mean用法及代碼示例
- Python pandas.core.groupby.DataFrameGroupBy.hist用法及代碼示例
- Python pandas.core.groupby.SeriesGroupBy.nlargest用法及代碼示例
- Python pandas.core.window.expanding.Expanding.kurt用法及代碼示例
- Python pandas.core.window.expanding.Expanding.sum用法及代碼示例
- Python pandas.core.window.expanding.Expanding.median用法及代碼示例
- Python pandas.core.window.expanding.Expanding.std用法及代碼示例
- Python pandas.core.window.rolling.Window.std用法及代碼示例
- Python pandas.core.window.rolling.Rolling.aggregate用法及代碼示例
- Python pandas.core.window.expanding.Expanding.sem用法及代碼示例
- Python pandas.core.window.expanding.Expanding.corr用法及代碼示例
- Python pandas.core.groupby.DataFrameGroupBy.resample用法及代碼示例
- Python pandas.core.window.expanding.Expanding.aggregate用法及代碼示例
- Python pandas.core.resample.Resampler.transform用法及代碼示例
- Python pandas.core.window.expanding.Expanding.count用法及代碼示例
- Python pandas.core.window.rolling.Rolling.sum用法及代碼示例
注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.crosstab。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。