用法:
pandas.api.types.union_categoricals(to_union, sort_categories=False, ignore_order=False)
合並Categorical-like 的list-like,合並類別。
所有類別必須具有相同的 dtype。
- to_union:list-like
Categorical、CategoricalIndex 或 dtype='category' 的係列。
- sort_categories:布爾值,默認為 False
如果為 true,則結果類別將被 lexsorted,否則它們將按照它們在數據中出現的順序進行排序。
- ignore_order:布爾值,默認為 False
如果為真,分類的有序屬性將被忽略。結果是無序的分類。
- 分類的
- TypeError
所有輸入都沒有相同的 dtype
所有輸入都沒有相同的有序屬性
所有輸入都是有序的,並且它們的類別不相同
sort_categories=True 並且分類是有序的
- ValueError
通過的分類空列表
參數:
返回:
拋出:
注意:
要了解有關類別的更多信息,請參閱鏈接
例子:
>>> from pandas.api.types import union_categoricals
如果要組合不一定具有相同類別的分類,
union_categoricals
將組合 list-like 的分類。新類別將是被組合類別的並集。>>> a = pd.Categorical(["b", "c"]) >>> b = pd.Categorical(["a", "b"]) >>> union_categoricals([a, b]) ['b', 'c', 'a', 'b'] Categories (3, object):['b', 'c', 'a']
默認情況下,生成的類別將按照它們出現在數據的
categories
中的順序排列。如果要對類別進行 lexsorted,請使用sort_categories=True
參數。>>> union_categoricals([a, b], sort_categories=True) ['b', 'c', 'a', 'b'] Categories (3, object):['a', 'b', 'c']
union_categoricals
也適用於組合相同類別和訂單信息的兩個類別的情況(例如,您還可以使用append
來做什麽)。>>> a = pd.Categorical(["a", "b"], ordered=True) >>> b = pd.Categorical(["a", "b", "a"], ordered=True) >>> union_categoricals([a, b]) ['a', 'b', 'a', 'b', 'a'] Categories (2, object):['a' < 'b']
引發
TypeError
因為類別是有序的並且不相同。>>> a = pd.Categorical(["a", "b"], ordered=True) >>> b = pd.Categorical(["a", "b", "c"], ordered=True) >>> union_categoricals([a, b]) Traceback (most recent call last): ... TypeError:to union ordered Categoricals, all categories must be the same
0.20.0 版中的新函數
可以使用
ignore_ordered=True
參數組合具有不同類別或排序的有序分類。>>> a = pd.Categorical(["a", "b", "c"], ordered=True) >>> b = pd.Categorical(["c", "b", "a"], ordered=True) >>> union_categoricals([a, b], ignore_order=True) ['a', 'b', 'c', 'c', 'b', 'a'] Categories (3, object):['a', 'b', 'c']
union_categoricals
也適用於包含分類數據的CategoricalIndex
或Series
,但請注意,生成的數組將始終是普通的Categorical
>>> a = pd.Series(["b", "c"], dtype='category') >>> b = pd.Series(["a", "b"], dtype='category') >>> union_categoricals([a, b]) ['b', 'c', 'a', 'b'] Categories (3, object):['b', 'c', 'a']
相關用法
- Python pandas.api.types.is_timedelta64_ns_dtype用法及代碼示例
- Python pandas.api.types.is_sparse用法及代碼示例
- Python pandas.api.types.is_extension_array_dtype用法及代碼示例
- Python pandas.api.types.is_extension_type用法及代碼示例
- Python pandas.api.types.is_dict_like用法及代碼示例
- Python pandas.api.types.is_float_dtype用法及代碼示例
- Python pandas.api.types.is_datetime64_any_dtype用法及代碼示例
- Python pandas.api.types.is_hashable用法及代碼示例
- Python pandas.api.types.is_categorical用法及代碼示例
- Python pandas.api.types.is_scalar用法及代碼示例
- Python pandas.api.types.is_number用法及代碼示例
- Python pandas.api.types.is_signed_integer_dtype用法及代碼示例
- Python pandas.api.types.is_datetime64tz_dtype用法及代碼示例
- Python pandas.api.types.is_datetime64_ns_dtype用法及代碼示例
- Python pandas.api.types.is_datetime64_dtype用法及代碼示例
- Python pandas.api.types.is_timedelta64_dtype用法及代碼示例
- Python pandas.api.types.infer_dtype用法及代碼示例
- Python pandas.api.types.is_iterator用法及代碼示例
- Python pandas.api.types.is_numeric_dtype用法及代碼示例
- Python pandas.api.types.is_interval_dtype用法及代碼示例
注:本文由純淨天空篩選整理自pandas.pydata.org大神的英文原創作品 pandas.api.types.union_categoricals。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。