Note
Go to the end to download the full example code.
12.3.10.4.9. Reshaping data#
import pandas as pd
import numpy as np
tuples = list(
zip(
*[
["bar", "bar", "baz", "baz", "foo", "foo", "qux", "qux"],
["one", "two", "one", "two", "one", "two", "one", "two"],
]
)
)
index = pd.MultiIndex.from_tuples(tuples, names=["first", "second"])
dataFrame = pd.DataFrame(np.random.randn(8, 2), index=index, columns=["A", "B"])
dataFrame2 = dataFrame[:4]
Stack
stacked = dataFrame2.stack()
stacked.unstack()
stacked.unstack(1)
stacked.unstack(0)
Pivot tables
dataFrame = pd.DataFrame(
{
"A": ["one", "one", "two", "three"] * 3,
"B": ["A", "B", "C"] * 4,
"C": ["foo", "foo", "foo", "bar", "bar", "bar"] * 2,
"D": np.random.randn(12),
"E": np.random.randn(12),
}
)
pd.pivot_table(dataFrame, values="D", index=["A", "B"], columns=["C"])
Time series
indexData = pd.date_range("1/5/2022", periods=100, freq="S")
timestamps = pd.Series(np.random.randint(0, 500, len(indexData)), index=indexData)
timestamps.resample("5Min").sum()
C:\Workspace\itom_development_VS2019_Qt5.15.2_x64\itomProject\itom\demo\python_packages\pandas\demo_reshaping.py:53: FutureWarning:
'S' is deprecated and will be removed in a future version, please use 's' instead.
2022-01-05 24493
Freq: 5min, dtype: int32
timeStempsUTC = timestamps.tz_localize("UTC")
timeStempsUTC.tz_convert("US/Eastern")
2022-01-04 19:00:00-05:00 351
2022-01-04 19:00:01-05:00 77
2022-01-04 19:00:02-05:00 269
2022-01-04 19:00:03-05:00 415
2022-01-04 19:00:04-05:00 47
...
2022-01-04 19:01:35-05:00 357
2022-01-04 19:01:36-05:00 484
2022-01-04 19:01:37-05:00 162
2022-01-04 19:01:38-05:00 128
2022-01-04 19:01:39-05:00 340
Freq: s, Length: 100, dtype: int32
ps = timestamps.to_period()
ps.to_timestamp()
2022-01-05 00:00:00 351
2022-01-05 00:00:01 77
2022-01-05 00:00:02 269
2022-01-05 00:00:03 415
2022-01-05 00:00:04 47
...
2022-01-05 00:01:35 357
2022-01-05 00:01:36 484
2022-01-05 00:01:37 162
2022-01-05 00:01:38 128
2022-01-05 00:01:39 340
Freq: s, Length: 100, dtype: int32
prng = pd.period_range("1990Q1", "2000Q4", freq="Q-NOV")
ts = pd.Series(np.random.randn(len(prng)), prng)
ts.index = (prng.asfreq("M", "e") + 1).asfreq("H", "s") + 9
C:\Workspace\itom_development_VS2019_Qt5.15.2_x64\itomProject\itom\demo\python_packages\pandas\demo_reshaping.py:72: FutureWarning:
'H' is deprecated and will be removed in a future version, please use 'h' instead.
Total running time of the script: (0 minutes 0.042 seconds)