0

I'm trying to perform this process, imagine to have the following and I want to obtain col4. :

Col1 Col2 Col3 Col 4 Col 5
SF 123 QW QW, BF 1
SF 456 AF AF 2
SO xxx AF AF, BF 3
SO yyy GD GD 4
SF 123 BF QW, BF 1
RE xxx BF AF, BF 5

For the purpose of aggragation I'm using these 2 lines of code:

df[df['col1']!='SF'].groupby(['Col2']).agg({'Col3' : lambda x: ','.join(x.unique())})
df[df['col1']=='SF'].groupby(['Col2','Col5']).agg({'Col3':','.join})

But I don't know how to put them on df. I tried also a merge but didn't work. I only hope to have been clear!!

Thanks so much in advance

EDIT 1 Sorry for not being clear. Before to perform any line of code I have Col1, Col2, Col3, Col5. Col4 is the output I would like to obtain.

2
  • Can you add what you'd like your output to be Commented Oct 4, 2021 at 17:22
  • Which columns do you already have? And which do you want to add? Commented Oct 4, 2021 at 17:23

1 Answer 1

2

You transform instead of agg to assign back to the original DataFrame:

df["Col4"] = df.groupby("Col2")["Col3"].transform(lambda x: ", ".join(x.unique()))
Sign up to request clarification or add additional context in comments.

1 Comment

It returns with an error: "Length mismatch: Expected axis has 133028 elements, new values have 133243 elements". I I was thinking to rewrite the question and publish another one, since I could be clearer explaining the issue

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.