0

I have a table in pandas and I would like to transform the values in the column DE_ANALITO into columns whose values would be what is in the column DE_RESULTADO.

+----+----------------------------------+--------------------+----------------+
|    | ID_PACIENTE                      | DE_ANALITO         | DE_RESULTADO   |
|----+----------------------------------+--------------------+----------------|
|  0 | 3487791F44C34B421C932DC8616A8437 | Fosfatase Alcalina | 106            |
|  1 | 3487791F44C34B421C932DC8616A8437 | Gama-GT            | 33             |
|  2 | 3487791F44C34B421C932DC8616A8437 | ALT (TGP)          | 51             |
|  3 | 3487791F44C34B421C932DC8616A8437 | DHL                | 530            |
|  4 | 3487791F44C34B421C932DC8616A8437 | Proteína C-Reativa | 1,84           |
+----+----------------------------------+--------------------+----------------+

I try:

exames.pivot(index="ID_PACIENTE", columns="DE_ANALITO", values="DE_RESULTADO")

But the error returns:

ValueError: Index contains duplicate entries, cannot reshape

In the documentation of the pivot method, the pivot of a table with the same format as mine, with an index column with repeated values, is given as an example. I don't know what I'm doing wrong

  • Use `pivot_table` – jezrael Jan 21 '21 at 13:48
  • `df.pivot` is a method for pivoting tables without aggregations. Since you have duplicate indexes, I am assuming you want to aggregate the values for repeated indexes. Try using `pd.pivot_table` in this case - `pd.pivot_table(exames, index="ID_PACIENTE", columns="DE_ANALITO", values="DE_RESULTADO", aggfunc='sum')` – Akshay Sehgal Jan 21 '21 at 13:50

0 Answers0