Set custom true/false values
In Boolean columns, pandas
automatically recognizes certain values, like "TRUE" and 1, as True
, and others, like "FALSE" and 0, as False
. Some datasets, like survey data, can use unrecognized values, such as "Yes" and "No".
For practice purposes, some Boolean columns in the New Developer Survey have been coded this way. You'll make sure they're properly interpreted with the help of the true_values
and false_values
arguments.
pandas
is loaded as pd
. You can assume the columns you are working with have no missing values.
This is a part of the course
“Streamlined Data Ingestion with pandas”
Exercise instructions
- Load the Excel file, specifying
"Yes"
as a true value and"No"
as a false value.
Hands-on interactive exercise
Have a go at this exercise by completing this sample code.
# Load file with Yes as a True value and No as a False value
survey_subset = pd.read_excel("fcc_survey_yn_data.xlsx",
dtype={"HasDebt": bool,
"AttendedBootCampYesNo": bool},
____,
____)
# View the data
print(survey_subset.head())
This exercise is part of the course
Streamlined Data Ingestion with pandas
Learn to acquire data from common file formats and systems such as CSV files, spreadsheets, JSON, SQL databases, and APIs.
Automate data imports from that staple of office life, Excel files. Import part or all of a workbook and ensure boolean and datetime data are properly loaded, all while learning about how other people are learning to code.
Exercise 1: Introduction to spreadsheetsExercise 2: Get data from a spreadsheetExercise 3: Load a portion of a spreadsheetExercise 4: Getting data from multiple worksheetsExercise 5: Select a single sheetExercise 6: Select multiple sheetsExercise 7: Work with multiple spreadsheetsExercise 8: Modifying imports: true/false dataExercise 9: Set Boolean columnsExercise 10: Set custom true/false valuesExercise 11: Modifying imports: parsing datesExercise 12: Parse simple datesExercise 13: Get datetimes from multiple columnsExercise 14: Parse non-standard date formatsWhat is DataCamp?
Learn the data skills you need online at your own pace—from non-coding essentials to data science and machine learning.