I' m new at Python, I've just start learning. I'm supposed to clean this DB and substitute 'ct' with 'connecticut' but if I write this
df_cleaned['cust_state'] = df_cleaned['cust_state'].replace("ct","connecticut", regex=True)
it will substitute ALL CT in the DF with 'connecticut'. How can I say "JUST THAT SINGLE ct"?
thank you
array(['new york', 'new jersey', 'pennsylvania', 'ct', 'south carolina',
'michigan', 'connecticut', 'georgia', 'arizona', 'alabama', 'ohio',
'florida', 'maryland', 'texas', 'california', 'hawaii', 'oklahoma',
'new mexico', 'indiana', 'colorado', 'north carolina', 'tennessee',
'washington', 'oregon', 'louisiana', 'illinois', 'utah', 'vermont',
'minnesota', 'nevada', 'virginia', 'west virginia', 'idaho',
'montana', 'wyoming', 'arkansas', 'massachusetts', 'missouri',
'kansas', 'nebraska', 'wisconsin', 'north dakota', 'mississippi',
'kentucky', 'district of columbia', 'alaska', 'rhode island',
'maine', 'delaware', 'south dakota', 'iowa', 'new hampshire'],
dtype=object)
CodePudding user response:
Add start two anchors, start of line ^
and end of line $
to you regex:
df_cleaned['cust_state'].replace("^ct$", "connecticut", regex=True)
CodePudding user response:
One way would be to use loc
:
df_cleaned.loc[df_cleaned['cust_state'].eq('ct'), 'cust_state'] = 'connecticut'
Which will locate all rows where 'cust_state' equals 'ct', and replace it with 'connecticut'