Tim Peyer
Tim Peyer

Reputation: 13

Pivot rows depending on several columns

please help me. I have a Dataframe like below. It has 5 Columns.

If 3 Columns are equal: 'id','name', 'concentration', I want the column 'zytokin' to expand and carry the values 'si'.

So if the first 4 lines looks like (with slight adjustments for explanatory purposes):

df = {
"id": ["6", "6", "6","6"],
"name": ['Diclofenac', 'Diclofenac', 'Metamizol','Metamizol'],
"concentration": [1, 1, 10,10],
"zytokin": [GzB, IFNg, GzB,IFNg],
"si": [2.5, 0.79, 1.25,1.73],
}

I want it to look like this

df = {"id": ["6", "6"],"name": ['Diclofenac','Metamizol'],"concentration": [1,10],
 "GzB": [2.5, 1.25],
 "IFNg": [0.79,1.73],

 }

Anywhere where one of 'id','name', 'concentration' is not equal to another line it should not merge, instead become a new line.

The new dataframe would have as columns: 'id','name', 'concentration','GzB', 'IFNg', 'GL', 'IL-13', 'IL-5'. and will probably contain about 20 rows.

So far I have tried to use .pivot() method, but I haven't managed to achieve this goal.

Thank you very much for your help, it is much appreciated!

Below the complete dataframe:

df= {'id': {0: 6, 1: 6, 2: 6, 3: 6, 4: 6, 5: 6, 6: 6, 7: 6, 8: 6, 9: 6, 10: 6, 11: 6, 12: 6, 13: 6, 14: 6, 15: 6, 16: 6, 17: 6, 18: 6, 19: 6, 20: 6, 21: 6, 22: 6, 23: 6, 24: 6, 25: 6, 26: 6, 27: 6, 28: 6, 29: 6, 30: 6, 31: 6, 32: 6, 33: 6, 34: 6, 35: 6, 36: 6, 37: 6, 38: 6, 39: 6, 40: 6, 41: 6, 42: 6, 43: 6, 44: 6, 45: 7, 46: 7, 47: 7, 48: 7, 49: 7, 50: 7, 51: 7, 52: 7, 53: 7, 54: 7, 55: 7, 56: 7, 57: 7, 58: 7, 59: 7, 60: 7, 61: 7, 62: 7, 63: 7, 64: 7, 65: 7, 66: 7, 67: 7, 68: 7, 69: 7, 70: 7, 71: 7, 72: 7, 73: 7, 74: 7, 75: 7, 76: 7, 77: 7, 78: 7, 79: 7, 80: 7, 81: 7, 82: 7, 83: 7, 84: 7, 85: 7, 86: 7, 87: 7, 88: 7, 89: 7, 90: 8, 91: 8, 92: 8, 93: 8, 94: 8, 95: 8, 96: 8, 97: 8, 98: 8, 99: 8}, 'name': {0: 'Diclofenac', 1: 'Diclofenac', 2: 'Metamizol', 3: 'Metamizol', 4: 'Metamizol', 5: 'Metamizol', 6: 'Metamizol', 7: 'Metamizol', 8: 'Diclofenac', 9: 'Metamizol', 10: 'Diclofenac', 11: 'Metamizol', 12: 'Diclofenac', 13: 'Diclofenac', 14: 'Diclofenac', 15: 'Diclofenac', 16: 'Diclofenac', 17: 'Diclofenac', 18: 'Diclofenac', 19: 'Diclofenac', 20: 'Diclofenac', 21: 'Diclofenac', 22: 'Co-Paracetamol', 23: 'Diclofenac', 24: 'Co-Paracetamol', 25: 'Co-Paracetamol', 26: 'Co-Paracetamol', 27: 'Co-Paracetamol', 28: 'Co-Paracetamol', 29: 'Co-Paracetamol', 30: 'Co-Paracetamol', 31: 'Metamizol', 32: 'Co-Paracetamol', 33: 'Metamizol', 34: 'Co-Paracetamol', 35: 'Co-Paracetamol', 36: 'Co-Paracetamol', 37: 'Co-Paracetamol', 38: 'Co-Paracetamol', 39: 'Metamizol', 40: 'Metamizol', 41: 'Metamizol', 42: 'Metamizol', 43: 'Co-Paracetamol', 44: 'Metamizol', 45: 'Ciprofloxacin', 46: 'Metronidazol', 47: 'Metronidazol', 48: 'Ciprofloxacin', 49: 'Ciprofloxacin', 50: 'Ciprofloxacin', 51: 'Ciprofloxacin', 52: 'Ciprofloxacin', 53: 'Ciprofloxacin', 54: 'Ciprofloxacin', 55: 'Metronidazol', 56: 'Ciprofloxacin', 57: 'Ciprofloxacin', 58: 'Ciprofloxacin', 59: 'Ciprofloxacin', 60: 'Ciprofloxacin', 61: 'Ciprofloxacin', 62: 'Ciprofloxacin', 63: 'Ciprofloxacin', 64: 'Ciprofloxacin', 65: 'Ciprofloxacin', 66: 'Ciprofloxacin', 67: 'Metronidazol', 68: 'Ciprofloxacin', 69: 'Metronidazol', 70: 'Metronidazol', 71: 'Metronidazol', 72: 'Metronidazol', 73: 'Metronidazol', 74: 'Metronidazol', 75: 'Metronidazol', 76: 'Metronidazol', 77: 'Metronidazol', 78: 'Metronidazol', 79: 'Metronidazol', 80: 'Metronidazol', 81: 'Metronidazol', 82: 'Metronidazol', 83: 'Metronidazol', 84: 'Metronidazol', 85: 'Metronidazol', 86: 'Metronidazol', 87: 'Metronidazol', 88: 'Metronidazol', 89: 'Metronidazol', 90: 'Levetiracetanum', 91: 'Lamotrigine', 92: 'Lamotrigine', 93: 'Lamotrigine', 94: 'Lamotrigine', 95: 'Lamotrigine', 96: 'Lamotrigine', 97: 'Lamotrigine', 98: 'Levetiracetanum', 99: 'Levetiracetanum'}, 'concentration': {0: 50.0, 1: 5.0, 2: 10.0, 3: 1.0, 4: 1.0, 5: 1.0, 6: 1.0, 7: 1.0, 8: 50.0, 9: 10.0, 10: 50.0, 11: 10.0, 12: 50.0, 13: 10.0, 14: 10.0, 15: 10.0, 16: 10.0, 17: 10.0, 18: 5.0, 19: 5.0, 20: 5.0, 21: 50.0, 22: 10.0, 23: 5.0, 24: 50.0, 25: 50.0, 26: 50.0, 27: 50.0, 28: 50.0, 29: 10.0, 30: 10.0, 31: 10.0, 32: 10.0, 33: 50.0, 34: 1.0, 35: 1.0, 36: 1.0, 37: 1.0, 38: 1.0, 39: 50.0, 40: 50.0, 41: 50.0, 42: 10.0, 43: 10.0, 44: 50.0, 45: 1.0, 46: 0.1, 47: 0.1, 48: 10.0, 49: 10.0, 50: 10.0, 51: 10.0, 52: 10.0, 53: 1.0, 54: 1.0, 55: 0.1, 56: 1.0, 57: 0.01, 58: 0.1, 59: 0.1, 60: 0.1, 61: 0.1, 62: 0.1, 63: 0.01, 64: 0.01, 65: 0.01, 66: 1.0, 67: 50.0, 68: 0.01, 69: 100.0, 70: 100.0, 71: 100.0, 72: 0.1, 73: 50.0, 74: 100.0, 75: 50.0, 76: 50.0, 77: 50.0, 78: 1.0, 79: 0.1, 80: 100.0, 81: 10.0, 82: 1.0, 83: 1.0, 84: 1.0, 85: 10.0, 86: 10.0, 87: 10.0, 88: 10.0, 89: 1.0, 90: 0.1, 91: 50.0, 92: 10.0, 93: 10.0, 94: 50.0, 95: 50.0, 96: 50.0, 97: 50.0, 98: 0.1, 99: 0.1}, 'zytokin': {0: 'GzB', 1: 'IFNg', 2: 'GL', 3: 'GL', 4: 'IFNg', 5: 'IL-13', 6: 'IL-5', 7: 'GzB', 8: 'GL', 9: 'IFNg', 10: 'IL-13', 11: 'IL-13', 12: 'IFNg', 13: 'GzB', 14: 'IFNg', 15: 'IL-13', 16: 'IL-5', 17: 'GL', 18: 'GL', 19: 'IL-5', 20: 'GzB', 21: 'IL-5', 22: 'GL', 23: 'IL-13', 24: 'GL', 25: 'IL-5', 26: 'IFNg', 27: 'GzB', 28: 'IL-13', 29: 'IL-5', 30: 'IFNg', 31: 'GzB', 32: 'IL-13', 33: 'IL-13', 34: 'GL', 35: 'GzB', 36: 'IFNg', 37: 'IL-13', 38: 'IL-5', 39: 'IL-5', 40: 'GzB', 41: 'IFNg', 42: 'IL-5', 43: 'GzB', 44: 'GL', 45: 'IL-13', 46: 'IFNg', 47: 'IL-13', 48: 'GL', 49: 'GzB', 50: 'IFNg', 51: 'IL-13', 52: 'IL-5', 53: 'GzB', 54: 'IL-5', 55: 'GL', 56: 'GL', 57: 'GzB', 58: 'GL', 59: 'GzB', 60: 'IFNg', 61: 'IL-13', 62: 'IL-5', 63: 'IFNg', 64: 'IL-13', 65: 'IL-5', 66: 'IFNg', 67: 'GzB', 68: 'GL', 69: 'IL-13', 70: 'GL', 71: 'GzB', 72: 'IL-5', 73: 'GL', 74: 'IFNg', 75: 'IFNg', 76: 'IL-13', 77: 'IL-5', 78: 'IFNg', 79: 'GzB', 80: 'IL-5', 81: 'GzB', 82: 'IL-13', 83: 'GzB', 84: 'GL', 85: 'IL-13', 86: 'GL', 87: 'IFNg', 88: 'IL-5', 89: 'IL-5', 90: 'GL', 91: 'GL', 92: 'GzB', 93: 'GL', 94: 'GzB', 95: 'IFNg', 96: 'IL-13', 97: 'IL-5', 98: 'IL-13', 99: 'IL-5'}, 'si': {0: 2.49, 1: 0.79, 2: 1.25, 3: 1.37, 4: 2.07, 5: 0.59, 6: 1.08, 7: 1.27, 8: 1.07, 9: 2.12, 10: 0.75, 11: 0.66, 12: 1.62, 13: 0.99, 14: 1.02, 15: 0.64, 16: 1.08, 17: 1.17, 18: 1.15, 19: 0.66, 20: 1.08, 21: 0.99, 22: 2.02, 23: 0.56, 24: 1.14, 25: 0.71, 26: 0.72, 27: 0.97, 28: 0.6, 29: 0.94, 30: 3.82, 31: 0.89, 32: 0.69, 33: 0.87, 34: 1.74, 35: 1.53, 36: 1.45, 37: 0.5, 38: 0.96, 39: 1.54, 40: 1.82, 41: 3.68, 42: 1.04, 43: 2.48, 44: 1.34, 45: 1.13, 46: 0.06, 47: 1.54, 48: 0.97, 49: 0.13, 50: 0.03, 51: 0.84, 52: 1.06, 53: 0.18, 54: 1.28, 55: 1.06, 56: 0.85, 57: 0.35, 58: 0.94, 59: 0.22, 60: 0.07, 61: 0.86, 62: 0.87, 63: 0.03, 64: 0.78, 65: 0.95, 66: 0.06, 67: 0.25, 68: 1.21, 69: 1.08, 70: 0.96, 71: 0.46, 72: 1.61, 73: 1.01, 74: 0.13, 75: 0.05, 76: 1.03, 77: 0.91, 78: 0.09, 79: 0.44, 80: 0.82, 81: 0.2, 82: 1.11, 83: 0.43, 84: 0.82, 85: 0.76, 86: 1.05, 87: 0.01, 88: 0.89, 89: 1.35, 90: 0.94, 91: 1.72, 92: 3.18, 93: 1.55, 94: 5.64, 95: 1.8, 96: 2.34, 97: 1.49, 98: 0.75, 99: 1.15}}`

Upvotes: 0

Views: 23

Answers (0)

Related Questions