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()
A B
first second
bar one -0.554887 0.638492
two 1.603665 1.977195
baz one -0.433228 0.564101
two 0.739366 -0.816024


stacked.unstack(1)
second one two
first
bar A -0.554887 1.603665
B 0.638492 1.977195
baz A -0.433228 0.739366
B 0.564101 -0.816024


stacked.unstack(0)
first bar baz
second
one A -0.554887 -0.433228
B 0.638492 0.564101
two A 1.603665 0.739366
B 1.977195 -0.816024


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"])
C bar foo
A B
one A 0.427408 0.081922
B 0.732450 -0.332109
C -0.668688 -0.644812
three A 0.975939 NaN
B NaN 0.025847
C 0.445298 NaN
two A NaN 0.472312
B 1.680834 NaN
C NaN -1.301363


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)