I am splitting strings to rows by using comma as a delimiter.
for col in [col for col in df.loc[:,df.columns.str.contains(">")]]: #only on colnames containing ">"
df[col] = df[col].str.split(", ")
df = df.explode(col).reset_index(drop=True)
However, there are a three substrings where commas are appearing "naturally" and should not cause a split:
- Data related to sexual preferences, sex life, and/or sexual orientation
- Contract, salary and benefits
- Procurement, subcontracting and vendor management
I was thinking since there only are these three instances if there is a way make some exception using something like this: "preferences,", "sex life,", "Contract," and "Procurement,". Or some more elegant way of solving it?
Here is a example df:
df = pd.DataFrame({"col > 1": ["Personals, Financials, Data related to sexual preferences, sex life, and/or sexual orientation", "Personals, Financials", "Vendors, Procurement, subcontracting and vendor management"]})
And here is what it should output:
+-------------------------------------------------------------------------+
| col > 1 |
+-------------------------------------------------------------------------+
| Personals |
| Financials |
| Data related to sexual preferences, sex life, and/or sexual orientation |
| Personals |
| Financials |
| Vendors |
| Procurement, subcontracting and vendor management |
+-------------------------------------------------------------------------+