简体   繁体   中英

How to convert cells in a pandas data frame with multiple values to multiple rows?

I am facing an issue while developing a piece of code for the below function. I have a dataframe with the below values

Date Name UserId task Client duration
1/2/2022 'Alex, J' 101 'C' QAT 8
2/2/2022 'Alex, J' 101 'C' QAT 8
1/2/2022 'Marc, B' 'Marc, B' 102 102 'A' 'B' App Dev 8
2/2/2022 'Marc, B' 'Marc, B' 102 102 'A' 'B' App Dev 8

Now, I want to convert to the below dataframe.

Date Name UserId task Client duration
1/2/2022 'Alex, J' 101 'C' QAT 8
2/2/2022 'Alex, J' 101 'A' QAT 8
1/2/2022 'Marc, B' 102 'A' App 4
1/2/2022 'Marc, B' 102 'B' Dev 4
2/2/2022 'Marc, B' 102 'A' App 4
2/2/2022 'Marc, B' 102 'B' Dev 4

I want to separate out the values in Name, UserId, task and Client column and want to divide the duration by the number of tasks for a particular day.

For example, I had 2 tasks here ie A and B for the same day(1/2/2022). So i divided the duration of 8 by 2 and got 4 for each A and B.

I would request you to please help me in this. Thanks alot.

The key here is to convert your string in each row to list and then use explode :

# Sample data:
df = pd.DataFrame({'user':['102 102', '103 103'],
                   'task':["'A' 'B'", "'A' 'B'"],
                   'duration':[8, 8]})

# Convert to list 
df['user'] = df['user'].str.split(' ')
df['task'] = df['task'].str.split(' ')

# Split row to multiple rows base on list
df.explode(['user', 'task'])

df

If your string is more complicated to separated, consider using import re . Or you can view it here: How to group by columns and merge only the unique strings of another column which are separated by a delimiter?

I think the rest is easy and you can do it well

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM