Skip to content

Instantly share code, notes, and snippets.

@luisdelatorre012
Last active June 2, 2024 16:14
Show Gist options
  • Save luisdelatorre012/5af824dcfe0c7510227c4e82c8bf8e25 to your computer and use it in GitHub Desktop.
Save luisdelatorre012/5af824dcfe0c7510227c4e82c8bf8e25 to your computer and use it in GitHub Desktop.
Column pivoter
# You:
# - I have two columns of a pandas table.
# One column is id. the second is billing_branch, a list stored as a "#;" separated string.
# Show me pandas code to transform this into a two column table with id and billing_branch, with the list pivoted into one value per row.
import pandas as pd
# Sample DataFrame
data = {
'id': [1, 2, 3],
'billing_branch': ['branch1#;branch2#;branch3', 'branch4#;branch5', 'branch6']
}
df = pd.DataFrame(data)
# Split and explode in one step
df_exploded = df.assign(billing_branch=df['billing_branch'].str.split('#;')).explode('billing_branch').reset_index(drop=True).drop_duplicates()
"""
SQL version:
WITH SplitCTE AS (
SELECT
id,
CAST('<x>' + REPLACE(billing_branch, '#;', '</x><x>') + '</x>' AS XML) AS BranchesXML
FROM
BillingBranches
),
ExplodedCTE AS (
SELECT
id,
Branch.value('.', 'NVARCHAR(MAX)') AS billing_branch
FROM
SplitCTE
CROSS APPLY
BranchesXML.nodes('/x') AS Split(Branch)
)
SELECT
id,
billing_branch
FROM
ExplodedCTE
ORDER BY
id;
"""
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment