I have a data frame "New_data" structured like this
| Index | ID | Content |
|---|---|---|
| 0 | 001 | abc |
| 1 | 001 | def |
| 2 | 002 | ghi |
| 3 | 002 | lmn |
| 4 | 002 | opq |
| 5 | 003 | rst |
| 6 | 001 | uvz |
As you can see the column "ID" contains multiple IDs that are equal. I would like to create an additional column ('Merged content') that, in each row, contains the values of all the "Content" cells with the same value in the "ID" column. I would like something like this:
| Index | ID | Content | Merged content |
|---|---|---|---|
| 0 | 001 | abc | abc def uvz |
| 1 | 001 | def | abc def uvz |
| 2 | 002 | ghi | ghi lmn opq |
| 3 | 002 | lmn | ghi lmn opq |
| 4 | 002 | opq | ghi lmn opq |
| 5 | 003 | rst | rst |
| 6 | 001 | uvz | abc def uvz |
I have tried with the for loops but I was not successful.
I have created a new df num with unique values for the ID column to iterate over them and about the problem of duplicated indexes but still, I was not able to solve my problem.
This is the code I have tried to apply:
for i in range(len(num)):
ID = num.iloc[i,0]
for j in range(len(new_data)):
ID_data = new_data.iloc[j,index]
if ID == ID_data.iloc[j, 0]:
new_data['Merged Content'] = new_data['Merged Content'].append(new_data['Content'])
I get this error:
AttributeError: 'numpy.int64' object has no attribute 'iloc'
Can you help me?