Ciaren

One Row per Tag While Preserving Record IDs

Each article has a tags field such as news;sports, and you want one row per tag so you can count or join on tags. Split to Rows splits the text and gives each piece its own row, repeating the other columns. It adds rows; Split a Column adds columns instead.

You'll use: File Input → Split to Rows → String Transform → File Output.

  1. 1Input
    File Input
    articles.csv
  2. 2Transform
    Split to Rows
    tags · delimiter ;
  3. 3Transform
    String Transform
    tags · Strip spaces
  4. 4Output
    File Output
    article_tags

Sample data

Save this as articles.csv. Record r3 has no tags, r4 ends with a delimiter, and r5 has a space after the delimiter.

csv
record_id,tags
r1,news;sports
r2,news
r3,
r4,a;
r5,news; sports

Steps

  1. On Datasets, upload articles.csv.
  2. In a new flow, add File Input. Set File type to CSV and Dataset to articles.csv.
  3. Add Split to Rows. Set Column to tags and Delimiter (optional) to ;.
  4. Add String Transform. Set Column to tags and click Strip spaces. Without it, r5 gets the tag " sports", which does not match sports.
  5. Add File Output. Set File type to CSV (.csv) and Dataset name to article_tags. Connect the nodes in order and click Run.

Split to Rows turns five records into eight rows: each tag gets its own row with the record_id repeated, a null stays one null row, a trailing delimiter adds an empty tag, and a space after the delimiter stays in the tag

Result

Five records become eight rows:

record_idtags
r1news
r1sports
r2news
r3null
r4a
r4empty text ("")
r5news
r5sports

Check the result

Save the flow, click Preview, select the String Transform node and click Run preview. You should see 8 rows, with r1 and r5 on two rows each. Switch to Profile: tags should list news 3 times and sports 2 times. If sports appears only once, the Strip spaces step is missing.

Pitfalls

  • A null stays one row. r3 keeps one row with a null tag, so the record does not disappear.
  • A trailing delimiter adds an empty tag. a; gives a and an empty text. Remove such rows with Filter by Expression, for example tags != '', which drops the empty tag and keeps the null row.
  • Null and empty text look alike in a CSV. Both are written as an empty cell by the pandas engine; Polars writes the empty text as "". Check the preview, not the file.
  • Pick the delimiter the data uses. With the default ,, nothing in this sample is split.

Exported Python

Click Export in the editor toolbar. The pandas and polars tabs show this code for the flow. It reads articles.csv from the script's folder and writes article_tags.csv.

python
import pandas as pd

df_articles = pd.read_csv('articles.csv')

df_articles = (
    df_articles.assign(tags=lambda _d: _d['tags'].astype('string').str.split(';'))
    .explode('tags')
    .reset_index(drop=True)
    .assign(tags=lambda _d: _d['tags'].astype('string').str.strip())
)

df_articles.to_csv('article_tags.csv', index=False)

See also