0

I have a dataframe. It looks a bit like this:

temp = pd.DataFrame()
dummy_cols = ['type_1','type_2','type_3','type_4']

temp['title'] = ['a', 'b', 'a', 'c']
temp['type_1'] = [1, 0, 0, 0]
temp['type_2'] = [0, 0, 1, 1]
temp['type_3'] = [0, 1, 0, 0]
temp['type_4'] = [0, 0, 0, 1]


╔════╦═════════════╦════════╦════════╦════════╦════════╗
║    ║ title       ║ type_1 ║ type_2 ║ type_3 ║ type_4 ║
╠════╬═════════════╬════════╬════════╬════════╬════════╣
║ 0  ║      a      ║   1    ║   0    ║   0    ║   0    ║
╠════╬═════════════╬════════╬════════╬════════╬════════╣
║ 1  ║      b      ║   0    ║   0    ║   1    ║   1    ║
╠════╬═════════════╬════════╬════════╬════════╬════════╣
║ 2  ║      a      ║   0    ║   1    ║   0    ║   0    ║
╠════╬═════════════╬════════╬════════╬════════╬════════╣
║ 3  ║      c      ║   0    ║   0    ║   0    ║   1    ║
╚════╩═════════════╩════════╩════════╩════════╩════════╝

I would like to merge rows with matching titles, and to combine their respective 1s. The result should look like this:

╔════╦═════════════╦════════╦════════╦════════╦════════╗
║    ║ title       ║ type_1 ║ type_2 ║ type_3 ║ type_4 ║
╠════╬═════════════╬════════╬════════╬════════╬════════╣
║ 0  ║      a      ║   1    ║   1    ║   0    ║   0    ║
╠════╬═════════════╬════════╬════════╬════════╬════════╣
║ 1  ║      b      ║   0    ║   0    ║   1    ║   1    ║
╠════╬═════════════╬════════╬════════╬════════╬════════╣
║ 2  ║      c      ║   0    ║   0    ║   0    ║   1    ║
╚════╩═════════════╩════════╩════════╩════════╩════════╝
Henry Dashwood
  • 143
  • 1
  • 2
  • 13

0 Answers0