Page 232 - Touhpad Ai
P. 232
Output:
Salary Salary_Scaled
0 20000 0.00
1 30000 0.25
2 40000 0.50
3 50000 0.75
4 60000 1.00
Beside these methods, you can also standardised the text and date format.
For Text Format Standardisation
Text format standardisation ensures that all text values follow a consistent style.
Program 34: To standardise the different text formats into a common format
import pandas as pd
# Sample data
df = pd.DataFrame({'Response': ['YES', 'yes', 'Y', 'No', 'nO']})
# Standardise response
df['Standard_Response'] = df['Response'].str.lower().replace({'y': 'yes', 'n':
'no'}).str.capitalize()
print(df)
Output:
Response Standard_Response
0 YES Yes
1 yes Yes
2 Y Yes
3 No No
4 nO No
For Date Format Standardisation
Date format standardisation ensures that all dates follow a consistent style.
Program 35: To standardise the different date formats into a common format (DD-MM-YYYY)
import pandas as pd
# Sample data with mixed date formats
df = pd.DataFrame({'Date': ['2025/08/05', '05-09-2025', '08.07.2025']})
# Convert to datetime and standardise format
df['Standard_Date'] = pd.to_datetime(df['Date'], format='mixed', dayfirst=True).
dt.strftime('%d-%m-%Y')
Output:
Date Standard_Date
0 2025/08/05 05-08-2025
1 05-09-2025 05-09-2025
2 08.07.2025 08-07-2025
230 Touchpad Artificial Intelligence - XI

