F. Jury F. Jury - 2 months ago 6
Python Question

Make a table from 2 columns

I'm fairly new on Python.
I have 2 columns on a dataframe, columns are something like:

C 4
C 5
A 1
B 6
B 1
A 2
C 4


I need them to be like this:

1 2 3 4 5 6
A 1 1 0 0 0 0
B 1 0 0 0 0 1
C 0 0 0 2 1 0


so they act like rows and columns and values refer to the number of coincidences.

please help!

edit: before this, there is only the code to read the data and select both columns

db = pd.read_excel(path_to_file/file.xlsx)
db = db.loc[:,['col1','col2']]

Answer

Say your columns are called cat and val:

In [26]: df = pd.DataFrame({'cat': ['C', 'C', 'A', 'B', 'B', 'A', 'C'], 'val': [4, 5, 1, 6, 1, 2, 4]})

In [27]: df
Out[27]: 
  cat  val
0   C    4
1   C    5
2   A    1
3   B    6
4   B    1
5   A    2
6   C    4

Then you can groupby the table hierarchicaly, then unstack it:

In [28]: df.val.groupby([df.cat, df.val]).sum().unstack().fillna(0).astype(int)
Out[28]: 
val  1  2  4  5  6
cat               
A    1  2  0  0  0
B    1  0  0  0  6
C    0  0  8  5  0

Edit

As IanS pointed out, 3 is missing here (thanks!). If there's a range of columns you must have, then you can use

r = df.val.groupby([df.cat, df.val]).sum().unstack().fillna(0).astype(int)

for c in set(range(1, 7)) - set(df.val.unique()):
    r[c] = 0