用法:
dask.dataframe.reshape.get_dummies(data, prefix=None, prefix_sep='_', dummy_na=False, columns=None, sparse=False, drop_first=False, dtype=<class 'numpy.uint8'>, **kwargs)
將分類變量轉換為虛擬/指標變量。
數據必須具有類別 dtype 才能推斷結果的
columns
。- data:係列或 DataFrame
對於 Series,dtype 必須是分類的。對於 DataFrame,至少有一列必須是分類的。
- prefix:字符串、字符串列表或字符串字典,默認無
附加 DataFrame 列名的字符串。在 DataFrame 上調用 get_dummies 時,傳遞一個長度等於列數的列表。或者,
prefix
可以是將列名映射到前綴的字典。- prefix_sep:字符串,默認 ‘_’
如果附加前綴,則使用分隔符/定界符。或者像
prefix.
一樣傳遞一個列表或字典- dummy_na:布爾值,默認為 False
如果忽略 False NaN,則添加一列以指示 NaN。
- columns:list-like,默認無
要編碼的 DataFrame 中的列名。如果
columns
為 None 則所有具有category
dtype 的列都將被轉換。- sparse:布爾值,默認為 False
虛擬列是否應該稀疏。如果
data
是一個係列或者如果包含所有列,則返回 SparseDataFrame。否則返回帶有一些 SparseBlocks 的 DataFrame。- drop_first:布爾值,默認為 False
是否通過刪除第一級從 k 個分類級別中取出 k-1 個虛擬變量。
- dtype:dtype,默認 np.uint8
新列的數據類型。隻允許使用一個 dtype。
- dummies: DataFrame
參數:
返回:
例子:
Dask 的版本僅適用於分類數據,因為這是在不計算所有數據的情況下了解輸出形狀的唯一方法。
>>> import pandas as pd >>> import dask.dataframe as dd >>> s = dd.from_pandas(pd.Series(list('abca')), npartitions=2) >>> dd.get_dummies(s) Traceback (most recent call last): ... NotImplementedError: `get_dummies` with non-categorical dtypes is not supported...
使用分類數據:
>>> s = dd.from_pandas(pd.Series(list('abca'), dtype='category'), npartitions=2) >>> dd.get_dummies(s) Dask DataFrame Structure: a b c npartitions=2 0 uint8 uint8 uint8 2 ... ... ... 3 ... ... ... Dask Name: get_dummies, 4 tasks >>> dd.get_dummies(s).compute() a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0
相關用法
- Python dask.dataframe.read_table用法及代碼示例
- Python dask.dataframe.read_hdf用法及代碼示例
- Python dask.dataframe.read_json用法及代碼示例
- Python dask.dataframe.read_fwf用法及代碼示例
- Python dask.dataframe.read_sql_table用法及代碼示例
- Python dask.dataframe.read_parquet用法及代碼示例
- Python dask.dataframe.read_csv用法及代碼示例
- Python dask.dataframe.read_orc用法及代碼示例
- Python dask.dataframe.rolling.Rolling.var用法及代碼示例
- Python dask.dataframe.rolling.Rolling.count用法及代碼示例
- Python dask.dataframe.rolling.Rolling.min用法及代碼示例
- Python dask.dataframe.rolling.Rolling.quantile用法及代碼示例
- Python dask.dataframe.rolling.Rolling.std用法及代碼示例
- Python dask.dataframe.rolling.Rolling.sum用法及代碼示例
- Python dask.dataframe.rolling.Rolling.kurt用法及代碼示例
- Python dask.dataframe.rolling.Rolling.mean用法及代碼示例
- Python dask.dataframe.rolling.Rolling.median用法及代碼示例
- Python dask.dataframe.Series.apply用法及代碼示例
- Python dask.dataframe.to_records用法及代碼示例
- Python dask.dataframe.DataFrame.applymap用法及代碼示例
注:本文由純淨天空篩選整理自dask.org大神的英文原創作品 dask.dataframe.reshape.get_dummies。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。