In the index of the
Series
,
Tens
is level 0 and
Ones
is level 1.
Ones
is the
last
or
innermost
level of the index.
import sys
import pandas as pd
nrows = 3
ncols = 4
rangeOfRows = range(10, 10 * (nrows + 1), 10) #count by 10s
rangeOfCols = range(1, ncols + 1) #count by 1s
index = [
[row for row in rangeOfRows for _ in rangeOfCols],
nrows * list(rangeOfCols)
]
data = [tens + ones for tens, ones in zip(*index)]
series = pd.Series(data = data, index = index, name = "Sums")
series.index.names = ["Tens", "Ones"]
print(series)
print()
df = series.unstack()
print(df)
print()
series = df.stack()
print(series)
sys.exit(0)
Tens Ones 10 1 11 2 12 3 13 4 14 20 1 21 2 22 3 23 4 24 30 1 31 2 32 3 33 4 34 Name: Sums, dtype: int64 Ones 1 2 3 4 Tens 10 11 12 13 14 20 21 22 23 24 30 31 32 33 34 Tens Ones 10 1 11 2 12 3 13 4 14 20 1 21 2 22 3 23 4 24 30 1 31 2 32 3 33 4 34 dtype: int64
import sys import string import functools import numpy as np import pandas as pd import roman_numerals levels = [ ["Romans", 2], ["Letters", 2], #how many letters per Roman numeral ["Numbers", 2] #how many numbers per letter ] nRomans = levels[0][1] nLetters = levels[1][1] #letters per roman nNumbers = levels[2][1] #numbers per letter romans = [ roman_numerals.convert_to_numeral(i) for i in range(1, nRomans + 1) for j in range(nLetters) for k in range(nNumbers) ] letters = [ letter for i in range(1, nRomans + 1) for letter in string.ascii_uppercase[:nLetters] for k in range(nNumbers) ] numbers = [ number for i in range(1, nRomans + 1) for j in range(nLetters) for number in list(range(1, nNumbers + 1)) ] listOfLists = [ romans, letters, numbers ] names = [level[0] for level in levels] counts = [level[1] for level in levels] nRows = functools.reduce(lambda product, factor: product * factor, counts, 1) multiIndex = pd.MultiIndex.from_arrays(listOfLists, names = names) series = pd.Series( data = np.arange(nRows), index = multiIndex, name = "Outline" ) print(series) sys.exit(0)
Romans Letters Numbers Ⅰ A 1 0 2 1 B 1 2 2 3 Ⅱ A 1 4 2 5 B 1 6 2 7 Name: Outline, dtype: int64