r/learnpython • u/Dragoran21 • 20m ago
Looking a way to combine columns based on values.
I wish to alter a table/dataframe like this one:
| Type | Alpha | Type | Beta | Type | Gamma |
|---|---|---|---|---|---|
| A | 100 | A | 80 | A | 90 |
| B | 20 | B | 40 | B | 30 |
| C | 1 | C | 1 | C | 0 |
| X | 1 | Y | 2 | Z | 1 |
Into something looking like this one:
| Type | Alpha | Beta | Gamma |
|---|---|---|---|
| A | 100 | 80 | 90 |
| B | 20 | 20 | 30 |
| C | 1 | 1 | 0 |
| X | 1 | nan | nan |
| Y | nan | 2 | nan |
| Z | nan | nan | 1 |
To combine data based on shared values so it is easier to compare different types.
How could I do this with Python?
1
Upvotes
•
u/seanv507 3m ago
have you asked google ai? use pandas or polars
create 3 dataframes from each pair of columns, renaming/formatting to type| key|value (where key = Alpha/Beta/Gamma for each corresponding dataframe)
then concatenate the 3 dataframes
then pivot on key