You can use the crosstab function to do this:
In [14]: pd.crosstab(index=df['values'], columns=[df['convert_me'], df['age_col']]) Out[14]: convert_me Convert1 Convert2 Convert3 age_col 23 33 44 values 21.71502 1 0 0 58.35506 0 1 0 60.41639 0 0 1
or pivot_table (with len as an aggregate function, but here you have to fillna use NaN with zeros manually):
In [18]: df.pivot_table(index=['values'], columns=['age_col', 'convert_me'], aggfunc=len).fillna(0) Out[18]: age_col 23 33 44 convert_me Convert1 Convert2 Convert3 values 21.71502 1 0 0 58.35506 0 1 0 60.41639 0 0 1
See the docs here: http://pandas.pydata.org/pandas-docs/stable/reshaping.html#pivot-tables-and-cross-tabulations
Most functions in pandas will return a multi-level (hierarchical) index, in this case for columns. If you want to "melt" this one level, as in R, you can do:
In [15]: df_cross = pd.crosstab(index=df['values'], columns=[df['convert_me'], df['age_col']]) In [16]: df_cross.columns = ["{0}_{1}".format(l1, l2) for l1, l2 in df_cross.columns] In [17]: df_cross Out[17]: Convert1_23 Convert2_33 Convert3_44 values 21.71502 1 0 0 58.35506 0 1 0 60.41639 0 0 1
source share