With pandas 0.19, you can do that in a single line :
pd.get_dummies(data=df, columns=['A', 'B'])
Columns
specifies where to do the One Hot Encoding.
>>> df
A B C
0 a c 1
1 b c 2
2 a b 3
>>> pd.get_dummies(data=df, columns=['A', 'B'])
C A_a A_b B_b B_c
0 1 1.0 0.0 0.0 1.0
1 2 0.0 1.0 0.0 1.0
2 3 1.0 0.0 1.0 0.0
Related Contents:
- Adding dummy columns to the original dataframe
- How do I select rows from a DataFrame based on column values?
- Selecting multiple columns in a Pandas dataframe
- Combining two Series into a DataFrame in pandas
- Add column to dataframe with constant value
- Filtering Pandas DataFrames on dates
- How to get rid of “Unnamed: 0” column in a pandas DataFrame read in from CSV file?
- How to filter rows containing a string pattern from a Pandas dataframe [duplicate]
- How to show all columns’ names on a large pandas dataframe?
- Can pandas automatically read dates from a CSV file?
- How to “select distinct” across multiple data frame columns in pandas?
- What is dtype(‘O’), in pandas?
- pandas how to check dtype for all columns in a dataframe?
- How to merge multiple dataframes
- Move column by name to front of table in pandas
- What values are valid in Pandas ‘Freq’ tags?
- How to shift a column in Pandas DataFrame
- Pandas selecting by label sometimes return Series, sometimes returns DataFrame
- Pandas DataFrame stored list as string: How to convert back to list
- Display rows with one or more NaN values in pandas dataframe
- Group dataframe and get sum AND count?
- Python pandas: how to specify data types when reading an Excel file?
- Retrieve DataFrame of all but one specified column [duplicate]
- How to append rows in a pandas dataframe in a for loop?
- Finding common rows (intersection) in two Pandas dataframes
- Merge multiple column values into one column in python pandas
- How to simply add a column level to a pandas dataframe
- Pandas – Compute z-score for all columns
- Count frequency of values in pandas DataFrame column
- Python / Pandas – GUI for viewing a DataFrame or Matrix [closed]
- How to delete multiple pandas (python) dataframes from memory to save RAM?
- UnicodeDecodeError when reading CSV file in Pandas
- Create a set from a series in pandas
- how to read certain columns from Excel using Pandas – Python
- Numpy “where” with multiple conditions
- return max value from pandas dataframe as a whole, not based on column or rows
- df.unique() on whole DataFrame based on a column
- Set MultiIndex of an existing DataFrame in pandas
- How do I assign values based on multiple conditions for existing columns?
- How can I use cumsum within a group in Pandas?
- Pandas DataFrame: apply function to all columns
- Returning multiple values from pandas apply on a DataFrame
- How to select a range of values in a pandas dataframe column?
- How to pivot a dataframe in Pandas? [duplicate]
- Pandas groupby with bin counts
- which is faster for load: pickle or hdf5 in python
- Normalizing pandas DataFrame rows by their sums
- Reshape wide to long in pandas
- Pandas KeyError: value not in index
- What is the fastest way to upload a big csv file in notebook to work with python pandas?