1

I have a pandas DataFrame which is grouped by p_id. The goal is to get a DataFrame with data shown under 'Output I'm looking for'. I've tried a few things, but I am struggling applying two custom aggregated functions:

  • apply(list) for x_id
  • '||'.join for x_name.

How can I solve this problem?

Input

| p_id | x_id | x_name |
|------|------|--------|
| 1    | 4    | Text   |
| 2    | 4    | Text   |
| 2    | 5    | Text2  |
| 2    | 6    | Text3  |
| 3    | 4    | Text   |
| 3    | 7    | Text4  |

Output I'm looking for

| p_id | x_ids   | x_names            |
|------|---------|--------------------|
| 1    | [4]     | Text               |
| 2    | [4,5,6] | Text||Text2||Text3 |
| 3    | [4,7]   | Text||Text4        |

1 Answer 1

1

You can certainly do:

df.groupby('pid').agg({'x_id':list, 'x_name':'||'.join})

Or a little more advanced with named agg:

df.groupby('pid').agg(x_ids=('x_id',list),
                      x_names=('x_name', '||'.join))
Sign up to request clarification or add additional context in comments.

1 Comment

Perfect! I didn't know you could use custom functions with agg! :)

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.