1
name           text       group
a|b            a test     m|l|n

I have a DataFrame like above. If there is a delimiter in a column value, I want to split it and put it in a separate line.

columns = ['name', 'text', 'group']            
for column in columns:
   if column == 'name' and column in df:
      df = df.assign(name=df.name.str.split(delimiter)).explode(column)

The problem with this code is that, I have to use multiple if to test the actual column name string, i.e. 'name'. I want to a general way like below:

if column in df:
   df = df.assign(column=df.column.str.split(delimiter)).explode(column)

But this is invalid. Any walk-around to do this?

1 Answer 1

2

Use [] instead dot notation:

delimiter = '|'
column = 'group'

if column in df:
    df = df.assign(**{column:df[column].str.split(delimiter)}).explode(column)
    print (df)
  name    text group
0  a|b  a test     m
0  a|b  a test     l
0  a|b  a test     n

Another idea if need exploding multiple columns:

#get values from columns list if exist in df.columns
cols = df.columns.intersection(columns)         
print (cols)

#assign back splitted columns by dict comprehension and explode by all columns in list cols
df = df.assign(**{x: df[x].str.split(delimiter) for x in cols}).explode(cols)
Sign up to request clarification or add additional context in comments.

8 Comments

Let me try it! If the first way works, that would be simplest.
But I got this error message after I made the change: df = df.assign(column=df[column].str.split(delimiter)).explode(column) TypeError: unhashable type: 'list'
@marlon - Problem is use old pandas version, is possible upgrade to last?
Yes, I will try.
So that requires python3.8, right? I am using Python3.7 and will upgrade it first. Also, would your second way of exploding multiple columns be more efficient? The first method is quite slow when I have a lot of columns that need to be split.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.