Created
January 6, 2023 15:58
-
-
Save bennyistanto/773cad4ef9681cfd2803003cb2148c1c to your computer and use it in GitHub Desktop.
Reshape long Stata-style structure to wide column structure
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 | |
# Load the data from the output CSV file | |
df = pd.read_csv("output.csv") | |
# Pivot the data frame to wide format for the min variable | |
min_wide = df.pivot_table(index=["lon", "lat", "orig_id", "year", "month"], columns="variable", values="value") | |
# Reset the index to obtain a flat data frame | |
min_wide = min_wide.reset_index() | |
# Save the resulting data frame to a CSV file | |
min_wide.to_csv("min.csv", index=False) | |
# Pivot the data frame to wide format for the mean variable | |
mean_wide = df.pivot_table(index=["lon", "lat", "orig_id", "year", "month"], columns="variable", values="value") | |
# Reset the index to obtain a flat data frame | |
mean_wide = mean_wide.reset_index() | |
# Save the resulting data frame to a CSV file | |
mean_wide.to_csv("mean.csv", index=False) | |
# Pivot the data frame to wide format for the max variable | |
max_wide = df.pivot_table(index=["lon", "lat", "orig_id", "year", "month"], columns="variable", values="value") | |
# Reset the index to obtain a flat data frame | |
max_wide = max_wide.reset_index() | |
# Save the resulting data frame to a CSV file | |
max_wide.to_csv("max.csv", index=False) | |
""" | |
For standard wide structure all in single csv not separate as above output, use below code. | |
import pandas as pd | |
# Load the data from the output CSV file | |
df = pd.read_csv("output.csv") | |
# Pivot the data frame to wide format | |
df_wide = df.pivot_table(index=["lon", "lat", "orig_id", "year", "month"], columns="variable", values="value") | |
# Reset the index to obtain a flat data frame | |
df_wide = df_wide.reset_index() | |
# Save the resulting data frame to a CSV file | |
df_wide.to_csv("output_wide.csv", index=False) | |
""" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment