Data often has mistakes or different ways to write the same thing. Fixing these helps us get clear and correct results.
Handling inconsistent values in Pandas
df['column'] = df['column'].str.lower() df['column'] = df['column'].str.strip() df['column'] = df['column'].replace({'old_value': 'new_value'})
Use str.lower() or str.upper() to make text consistent in case.
Use str.strip() to remove unwanted spaces before or after text.
Use replace() to fix specific wrong or inconsistent values.
df['color'] = df['color'].str.lower()
df['country'] = df['country'].replace({'USA': 'United States', 'U.S.A.': 'United States'})
df['name'] = df['name'].str.strip()
This code fixes inconsistent country names by making them lowercase, removing spaces, and replacing variations of USA with 'united states'.
import pandas as pd data = {'Country': ['USA', 'usa ', 'U.S.A.', 'Canada', 'canada', 'CANADA ']} df = pd.DataFrame(data) # Make all country names lowercase df['Country'] = df['Country'].str.lower() # Remove extra spaces df['Country'] = df['Country'].str.strip() # Replace different USA forms with 'united states' df['Country'] = df['Country'].replace({'usa': 'united states', 'u.s.a.': 'united states'}) print(df)
Always check your data first to see what inconsistencies exist.
Use str.strip() to remove unwanted spaces that can cause mismatches.
Replacing values helps unify different spellings or abbreviations.
Inconsistent values can cause wrong analysis results.
Use string methods like lower(), upper(), and strip() to clean text data.
Use replace() to fix specific inconsistent values.
Practice
Solution
Step 1: Understand inconsistent values
Inconsistent values are errors or variations in data that can cause wrong analysis results.Step 2: Purpose of handling them
Cleaning these values ensures the data is uniform and trustworthy for analysis.Final Answer:
To clean data so analysis results are accurate and reliable -> Option AQuick Check:
Cleaning data = Accurate analysis [OK]
- Thinking inconsistent values add more data
- Confusing cleaning with sorting
- Assuming conversion to string fixes inconsistencies
Solution
Step 1: Identify method for replacing values
df.replace() is designed to substitute specified values with new ones.Step 2: Confirm other methods' purposes
df.dropna() removes missing data, df.sort_values() sorts data, df.groupby() groups data; none replace values.Final Answer:
df.replace() -> Option CQuick Check:
Replace values = df.replace() [OK]
- Using dropna() to replace values
- Confusing sorting with replacing
- Trying to group data to fix values
import pandas as pd
df = pd.DataFrame({'color': ['red', 'Red', 'blue', 'Blue', 'RED']})
df['color'] = df['color'].str.lower()What will be the unique values in
df['color'] after this code runs?Solution
Step 1: Understand str.lower() effect
str.lower() converts all strings in the column to lowercase.Step 2: Apply to values and find unique
All 'red', 'Red', 'RED' become 'red'; 'blue', 'Blue' become 'blue'. Unique values are ['red', 'blue'].Final Answer:
['red', 'blue'] -> Option AQuick Check:
Lowercase unique values = ['red', 'blue'] [OK]
- Thinking original case stays
- Assuming unique keeps all original variants
- Confusing str.lower() with str.upper()
df['gender'].replace({'M': 'Male', 'F': 'Female'})Why might it not update the DataFrame as expected?
Solution
Step 1: Check replace() behavior
By default, replace() returns a new Series and does not change df unless inplace=true or assigned.Step 2: Identify missing update step
The code does not assign back or use inplace=true, so df['gender'] remains unchanged.Final Answer:
Because replace() needs inplace=true or assignment to update df -> Option BQuick Check:
replace() without assignment = no change [OK]
- Assuming replace() changes df without assignment
- Thinking replace() can't use dict
- Believing replace() only works on numbers
Solution
Step 1: Normalize case to lowercase
Using str.lower() converts all variations like 'Active', 'ACTIVE' to 'active'.Step 2: Fix misspelled value
replace({'actve': 'active'}) corrects the misspelled 'actve' to 'active'.Step 3: Capitalize for consistent formatting
str.capitalize() changes 'active' to 'Active' for uniform appearance.Final Answer:
df['status'] = df['status'].str.lower().replace({'actve': 'active'}).str.capitalize() -> Option DQuick Check:
Lowercase + replace typo + capitalize = clean status [OK]
- Not fixing misspelled values
- Skipping case normalization
- Replacing with wrong values
Ready to prove it?
Pass the test and this pattern moves unknown
