Created
July 28, 2023 19:35
-
-
Save ayan-b/af4cbaeef20968ebfda49bf382487066 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pandas as pd | |
df = pd.DataFrame({ | |
"Column1": [1, 2, 3, 4, 5, 6, 7, 8], | |
"Column2": [9, 10, 11, 12, 13, 14, 15, 16], | |
"Column3": [17, 18, 19, 20, 21, 22, 23, 24], | |
"Column4": [25, 26, 27, 28, 29, 30, 31, 32], | |
"Column5": [33, 34, 35, 36, 37, 38, 39, 40], | |
"Column6": [41, 42, 43, 44, 45, 46, 47, 48], | |
"Column7": [49, 50, 51, 52, 53, 54, 55, 56], | |
"Column8": [57, 58, 59, 60, 61, 62, 63, 64] | |
}) | |
# Pivot only Column1 and Column2 | |
pivoted_df = df.pivot(index="Column1", columns="Column2", values="Column3") | |
# Add the rest of the columns to the pivoted DataFrame | |
unpivoted_df = df[[col for col in df.columns if col not in ["Column1", "Column2"]]] | |
pivoted_df = pd.concat([pivoted_df, unpivoted_df], axis=1) | |
print(pivoted_df) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment