I would like to reconstruct a dataframe from a contingency table stored as dataframe. For example from ctab
I would like to build df1
or df2
. Is there a command to do that
or do I need a loop?
import pandas as pd
ctab = pd.DataFrame([[1,2], [2, 1]], columns=["A", "B"], index=["A", "B"])
print(ctab)
df1 = pd.DataFrame([["A","A", 1], ["A","B", 2], ["B","A", 2], ["B","B", 1]], columns=["col", "index", "freq"])
print(df1)
df2 = pd.DataFrame([["A","A"], ["A","B"], ["A","B"], ["B","A"], ["B","A"], ["B","B"]], columns=["col", "index"])
print(df2)