1

How to modify dataframe with multiple values to have single values

Original input with multiple items/value

    category       item
0      vowel  A E I O U
1  consonant          B
2  consonant          C
3     number          1
4     number          2
5        mix      2a Z6

Expected modified dataframe

     category item
0       vowel    A
1       vowel    E
2       vowel    I
3       vowel    O
4       vowel    U
5   consonant    B
6   consonant    C
7      number    1
8      number    2
9         mix   2a
10        mix   Z6

1 Answer 1

3

Use DataFrame.explode with splitted values by whitespaces by Series.str.split:

df = df.assign(item = df['item'].str.split()).explode('item').reset_index(drop=True)
print (df)
     category item
0       vowel    A
1       vowel    E
2       vowel    I
3       vowel    O
4       vowel    U
5   consonant    B
6   consonant    C
7      number    1
8      number    2
9         mix   2a
10        mix   Z6
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks it works. By the way, when I got the numbers as NaN, [ 7 number NaN 8 number NaN ]. Anything need to set?

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.