I've got a problem with my python code. I'm trying to merge rows in my data frame. I've got something like that:
| CODE_ID | SIZE | 1 | 2 | 3 | 4 | ... | 18 | CODE |
|---|---|---|---|---|---|---|---|---|
| 12345 | Big | X | . | . | . | ... | . | abc |
| 12345 | Big | . | X | . | . | ... | . | def |
| 67890 | Medium | . | . | . | X | ... | . | ghi |
| 67890 | Medium | . | . | X | . | ... | . | jkl |
| 13579 | Small | . | . | . | X | ... | . | xyz |
I would like to merge the duplicates by CODE_ID, but strings in CODE column I'd like to join. Just like that:
| CODE_ID | SIZE | 1 | 2 | 3 | 4 | ... | 18 | CODE |
|---|---|---|---|---|---|---|---|---|
| 12345 | Big | X | X | . | . | ... | . | abc; def |
| 67890 | Medium | . | . | X | X | ... | . | ghi; jkl |
| 13579 | Small | . | . | . | X | ... | . | xyz |
I did it with agg, but it's just part of work.
g_df = df.groupby("CODE_ID")
group = g_df["CODE"].agg(lambda column: "; ".join(column))
group = group.reset_index(name="CODE")
Because the result is like that:
| CODE_ID | CODE |
|---|---|
| 12345 | abc; def |
| 67890 | ghi; jkl |
| 13579 | xyz |
How can I add the rest of columns? Or maybe it's the wrong way to do that?