- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python - Convert Pandas DataFrame to binary data
Use the get_dummies() method to convert categorical DataFrame to binary data. Following is our Pandas DataFrame with 2 columns −
dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', 'Kat'],"Result": ['Pass', 'Fail', 'Fail', 'Pass', 'Pass'] } )
Use the get_dummies() and set the column which you want to convert to binary form. Here, we want the Result in “Pass” and “Fail” form to be visible. Therefore, we will set the “Result” column −
pd.get_dummies(dataFrame["Result"]
Example
Following is the code −
import pandas as pd # Create DataFrame dataFrame = pd.DataFrame( { "Student": ['Jack', 'Robin', 'Ted', 'Scarlett', 'Kat'],"Result": ['Pass', 'Fail', 'Fail', 'Pass', 'Pass'] } ) print"DataFrame ...\n",dataFrame # converting to binary data dfBinary = pd.get_dummies(dataFrame["Result"]) print"\nDisplaying DataFrame in Binary form...\n",dfBinary
Output
This will produce the following output −
DataFrame ... Result Student 0 Pass Jack 1 Fail Robin 2 Fail Ted 3 Pass Scarlett 4 Pass Kat Displaying DataFrame in Binary form... Fail Pass 0 0 1 1 1 0 2 1 0 3 0 1 4 0 1
- Related Articles
- Python Pandas - Convert Nested Dictionary to Multiindex Dataframe
- Write a program in Python Pandas to convert a dataframe Celsius data column into Fahrenheit
- Python - Convert one datatype to another in a Pandas DataFrame
- Python – Reshape the data in a Pandas DataFrame
- Python - Convert list of nested dictionary into Pandas Dataframe
- Python Pandas - Plot multiple data columns in a DataFrame?
- Convert a Pandas DataFrame to a NumPy array
- Construct a DataFrame in Pandas using string data in Python
- Python Pandas - Convert string data into datetime type
- Python Pandas – How to use Pandas DataFrame Property: shape
- Python Pandas – How to use Pandas DataFrame tail( ) function
- How to check the data type in pandas DataFrame?
- How to convert a DataFrame into a dictionary in Pandas?
- Python – Merge two Pandas DataFrame
- Annotate data points while plotting from Pandas DataFrame

Advertisements