0

I've got a dataframe which looks like this:

Record Field11 ID LesionNumber Diagnosis1
1 False 1000 1 22
1 False 1000 2 88
1 False 1000 3 22
1 False 1000 4 24

All of the ID's are the same. And this kind of structure repeats for many different ID's.

Using all rows with the same ID, I'd like to create a new dataframe which looks like this:

Record ID LesionNumber Diagnosis1
1 1000 1, 2, 3, 4 22, 88, 22, 24

I'd like to have the LesionNumber and Diagnosis1 appear as ordered lists.

I'm new to Pandas and dataframes so my terminology may be off. Is this possible?

1 Answer 1

1

Using agg

df.groupby(['Record','Field11','ID']).agg(lambda x : ','.join(x.astype(str))).reset_index()
Out[634]: 
   Record  Field11    ID LesionNumber   Diagnosis1
0       1    False  1000      1,2,3,4  22,88,22,24
Sign up to request clarification or add additional context in comments.

Comments

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.