Clean Messy Text in a CSV
The email " [email protected] " does not match [email protected] in a join or
in Remove Duplicates, although both belong to the same person. Strip the
surrounding spaces, then lowercase the text, so equal addresses become equal
values.
You'll use: File Input → String Transform → String Transform → File Output.
- 1InputFile Inputcustomers.csv
- 2TransformString Transformemail · Strip spaces
- 3TransformString Transformemail · Lowercase
- 4OutputFile Outputcustomers_clean
Sample data
Save this as customers.csv. Customers 1 and 4 have the same address written
two ways, customer 3 has no email, and customer 5 has only spaces.
customer_id,email
1," [email protected] "
2,[email protected]
3,
4,[email protected]
5," "Steps
- On Datasets, upload
customers.csv. - In a new flow, add File Input. Set File type to CSV and Dataset
to
customers.csv. - Add String Transform. Set Column to
emailand click the Strip spaces operation. It removes spaces at the start and end of each value. - Add a second String Transform. Set Column to
emailand click Lowercase. One node applies one operation, so the two steps need two nodes. - Add File Output. Set File type to CSV (.csv) and Dataset name to
customers_clean. Connect the nodes in order and click Run.
Prefer to describe it?
With a provider key set up, upload the sample data, open a blank flow in the same project and ask the Flow assistant, for example: "trim the spaces in the email column and lowercase it, then write a CSV." Review the proposed nodes and settings, then choose Apply and save. We ran this prompt on the sample data; see what the assistant built. Its flow can differ from the steps above in node order and names, so check each setting. The assistant never runs a flow, so click Run yourself.
Result
| customer_id | |
|---|---|
| 1 | [email protected] |
| 2 | [email protected] |
| 3 | null |
| 4 | [email protected] |
| 5 | empty text ("") |
Check the result
Save the flow, click Preview, select the second String Transform node and
click Run preview. Customers 1 and 4 should show the same
[email protected]. If customer 1 still shows spaces or capitals, one of the
two String Transform nodes is not in the chain before the node you preview.
Their order does not matter.
Pitfalls
- Clean both sides of a join. Apply the same steps to the key column of each input before a Join, or the keys still differ.
- Only spaces become empty text, not null. Customer 5 ends up as
"", which Fill Nulls and Coalesce do not treat as missing. In a CSV output an empty text and a null can look the same, so check them in the preview. - Strip spaces does not touch inner spaces.
ada @example.comkeeps its space. Use the Find & Replace operation for characters inside the value. - Lowercase only where case carries no meaning. Keep passwords, tokens and case-sensitive IDs as they are. Cleaning does not check that an address is valid or deliverable.
Exported Python
Click Export in the editor toolbar. The pandas and polars tabs show
this code for the flow. It reads customers.csv from the script's folder and
writes customers_clean.csv.
import pandas as pd
df_customers = pd.read_csv('customers.csv')
df_customers = (
df_customers.assign(email=lambda _d: _d['email'].astype('string').str.strip())
.assign(email=lambda _d: _d['email'].astype('string').str.lower())
)
df_customers.to_csv('customers_clean.csv', index=False)