$ python3 pivot-pandas.py
date stock_code price
0 2018-08-27 001 10.0
1 2018-08-27 002 11.0
2 2018-08-27 003 12.0
3 2018-08-27 004 13.0
4 2018-08-26 001 14.0
5 2018-08-26 002 15.0
6 2018-08-26 003 11.0
7 2018-08-26 004 10.0
8 2018-08-26 005 19.0
Pivoted output
stock_code 001 002 003 004 005
date
2018-08-26 14.0 15.0 11.0 10.0 19.0
2018-08-27 10.0 11.0 12.0 13.0 NaN
Created
August 27, 2018 06:30
-
-
Save tuxdna/d6b16610e65e6c4ab05c70057518dbe8 to your computer and use it in GitHub Desktop.
Pivot example - stackoverflow.com/questions/52033359
This file contains 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 | |
import numpy as np | |
df = pd.read_csv("pivot.csv", dtype={"stock_code":np.str, "price": np.float64}, parse_dates=["date"]) | |
print(df) | |
print("Pivoted output") | |
df_by_codes = df.pivot(index="date", columns="stock_code", values="price") | |
print(df_by_codes) |
This file contains 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
date | stock_code | price | |
---|---|---|---|
20180827 | 001 | 10 | |
20180827 | 002 | 11 | |
20180827 | 003 | 12 | |
20180827 | 004 | 13 | |
20180826 | 001 | 14 | |
20180826 | 002 | 15 | |
20180826 | 003 | 11 | |
20180826 | 004 | 10 | |
20180826 | 005 | 19 |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment