官网:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html
2017-11-29 11:19
585 查看
官网:http://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html
pandas.
get_dummies(data,
prefix=None, prefix_sep='_', dummy_na=False, columns=None,
sparse=False, drop_first=False)[source]
Convert categorical variable into dummy/indicator variables
Parameters: | data : array-like, Series, or DataFrame prefix : string, list of strings, or dict of strings, default None String to append DataFrame column namesPass a list with length equal to the number of columnswhen calling get_dummies on a DataFrame. Alternatively, prefixcan be a dictionary mapping column names to prefixes. prefix_sep : string, default ‘_’ If appending prefix, separator/delimiter to use. Or pass alist or dictionary as with prefix. dummy_na : bool, default False Add a column to indicate NaNs, if False NaNs are ignored. columns : list-like, default None Column names in the DataFrame to be encoded.If columns is None then all the columns withobject or category dtype will be converted. sparse : bool, default False Whether the dummy columns should be sparse or not. ReturnsSparseDataFrame if data is a Series or if all columns are included.Otherwise returns a DataFrame with some SparseBlocks. drop_first : bool, default False Whether to get k-1 dummies out of k categorical levels by removing thefirst level. New in version 0.18.0. Returns ——- dummies : DataFrame or SparseDataFrame |
---|
Series.str.get_dummies
Examples
>>> import pandas as pd >>> s = pd.Series(list('abca'))
>>> pd.get_dummies(s) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0
>>> s1 = ['a', 'b', np.nan]
>>> pd.get_dummies(s1) a b 0 1 0 1 0 1 2 0 0
>>> pd.get_dummies(s1, dummy_na=True) a b NaN 0 1 0 0 1 0 1 0 2 0 0 1
>>> df = pd.DataFrame({'A': ['a', 'b', 'a'], 'B': ['b', 'a', 'c'], ... 'C': [1, 2, 3]})
>>> pd.get_dummies(df, prefix=['col1', 'col2']) C col1_a col1_b col2_a col2_b col2_c 0 1 1 0 0 1 0 1 2 0 1 1 0 0 2 3 1 0 0 0 1
>>> pd.get_dummies(pd.Series(list('abcaa'))) a b c 0 1 0 0 1 0 1 0 2 0 0 1 3 1 0 0 4 1 0 0
>>> pd.get_dummies(pd.Series(list('abcaa')), drop_first=True) b c 0 0 0 1 1 0 2 0 1 3 0 0 4 0 0
离散特征的编码分为两种情况:
1、离散特征的取值之间没有大小的意义,比如color:[red,blue],那么就使用one-hot编码
2、离散特征的取值有大小的意义,比如size:[X,XL,XXL],那么就使用数值的映射{X:1,XL:2,XXL:3}
使用pandas可以很方便的对离散型特征进行one-hot编码
[python] view plain copy
import pandas as pd
df = pd.DataFrame([
['green', 'M', 10.1, 'class1'],
['red', 'L', 13.5, 'class2'],
['blue', 'XL', 15.3, 'class1']])
df.columns = ['color', 'size', 'prize', 'class label']
size_mapping = {
'XL': 3,
'L': 2,
'M': 1}
df['size'] = df['size'].map(size_mapping)
class_mapping = {label:idx for idx,label in enumerate(set(df['class label']))}
df['class label'] = df['class label'].map(class_mapping)
说明:对于有大小意义的离散特征,直接使用映射就可以了,{'XL':3,'L':2,'M':1}
Using the get_dummies will create a new column for every unique string in a certain column:使用get_dummies进行one-hot编码
[python] view plain copy
pd.get_dummies(df)
相关文章推荐
- http://www.openwebx.org/docs/turbine.html
- Hadoop FS Shell命令 http://hadoop.apache.org/docs/stable/file_system_shell.html
- python3把httplib改了名字,对应的库是http.client https://docs.python.org/3.4/library/http.client.html https://do
- OPenCV在线文档http://docs.opencv.org/index.html
- http://www.faqs.org/docs/securing/index.html
- http://docs.python-requests.org/zh_CN/latest/user/quickstart.html
- http://construct.readthedocs.org/en/latest/basics.html
- HTTP协议与HTML表单(再谈GET与POST的区别)
- pandas使用get_dummies进行one-hot编码
- Get Response from Http (Referenced to http://topic.csdn.net/t/20050115/23/3729009.html)
- Object Model of JavaScript(http://developer.mozilla.org/en/docs/Core_JavaScript_1.5_Guide:Details_of_the_Object_Model)
- perl笔记--http://perldoc.perl.org/perlintro.html
- mysql 5.1.30 unknown option '--skip-federated' http://bbs.linuxtone.org/thread-1563-1-1.html
- Core Dump?! --http://docs.huihoo.com/gnu/linux/core.html
- Get Html Content use HttpDownloader
- android中Http访问时 connection.getResponseCode()不被执行,且报错 org.json.JSONException: End of input at character 0 of .
- PCA的数学原理(原文地址:http://blog.codinglabs.org/articles/pca-tutorial.html)
- http://www.cnblogs.com/nankezhishi/archive/2012/06/09/getandpost.html
- http://www.sci.muni.cz/docs/Unix/ksh.html
- 如何划分VLAN?有用的朋友可以看一下【转自:http://bbs.deepin.org/thread-1122906-1-1.html】