import pandas as pd
# First let's create a df which we want to correct:
df = pd.DataFrame(
{
"codes": [
"A1A1A1",
"A1A 1A1",
"A1A1a1",
"a1A 1A1",
]
}
)
# Now let's correct the postal code:
for current_index in df.index: # With this syntax you loop over each row
entry = df.loc[current_index, "codes"] # With .loc you can select a desired index and column (use the current_index from the loop)
entry = entry.upper() # Make it upper case
entry = entry.replace(" ", "") # Remove all the spaces
corrected_entry = entry[:3] + " " + entry[3:] # Now u assemble the new entry with "string slicing"
df.loc[current_index, "codes"] = corrected_entry # assign the corrected postal code to the data frame
result:
print(df)
codes
0 A1A 1A1
1 A1A 1A1
2 A1A 1A1
3 A1A 1A1