Notez que j'étais inquiet lorsque je voulais convertir les données de la grille.
Il y a de telles données de grille
import pandas as pd
df = pd.DataFrame(
np.arange(9).reshape(3, 3),
index=["y01", "y02", "y03"],
columns=["x01", "x02", "x03"]
)
| x01 | x02 | x03 | |
|---|---|---|---|
| y01 | 0 | 1 | 2 |
| y02 | 3 | 4 | 5 |
| y03 | 6 | 7 | 8 |
Je veux convertir ceci comme ça.
| column0 | column1 | column2 |
|---|---|---|
| y01 | x01 | 0 |
| y01 | x02 | 1 |
| y01 | x03 | 2 |
| y02 | x01 | 3 |
| y02 | x02 | 4 |
| y02 | x03 | 5 |
| y03 | x01 | 6 |
| y03 | x02 | 7 |
| y03 | x03 | 8 |
Chez les pandas, la méthode suivante est bonne.
df.stack().reset_index()
Ensuite, ça sort comme ça.
| level_0 | level_1 | 0 | |
|---|---|---|---|
| 0 | y01 | x01 | 0 |
| 1 | y01 | x02 | 1 |
| 2 | y01 | x03 | 2 |
| 3 | y02 | x01 | 3 |
| 4 | y02 | x02 | 4 |
| 5 | y02 | x03 | 5 |
| 6 | y03 | x01 | 6 |
| 7 | y03 | x02 | 7 |
| 8 | y03 | x03 | 8 |
Happy !! S'il vous plaît laissez-moi savoir s'il y a une meilleure façon
Recommended Posts