I have data frame like
County section
A S1,S2
C ALL
B S1
Expected Output
County section
A S1
A S2
C S1
C S2
B S2
My code
df =df.assign(sections=df.sections.replace({'ALL':df.loc[df.sections.str.split(',').str.len().idxmax(),'sections']}).str.split(',')).explode('sections')
But the above code only works when we have comma separated multiple sections (S1,S2,S3) . But doesn't work when we have section value without comma separated as shown below. How can make the code to work for both scenarios together
County Section
A ALL
B S1
C ALL
D ALL
Expected Output
County Section
A S1
B S1
C S1
D S1
Code should work in both scenarios