Make a table from 2 columns
<p>I'm fairly new on Python.</p>

<p>I have 2 columns on a dataframe, columns are something like: </p>

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

col1  col2
C     4
C     5
A     1
B     6
B     1
A     2
C     4
</code></pre>

<p>I need them to be like this:</p>

<pre><code>   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
</code></pre>

<p>so they act like rows and columns and values refer to the number of coincidences.</p>

<p>I think you need aggreagate by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.size.html" rel="nofollow"><code>size</code></a> and add missing values to columns by <a href="http://pandas.pydata.org/pandas-docs/stable/generated/pandas.Index.reindex.html" rel="nofollow"><code>reindex</code></a>:</p>

<pre><code>print (df)
   a  b
0  C  4
1  C  5
2  A  1
3  B  6
4  B  1
5  A  2
6  C  4

df1 = df.b.groupby([df.a, df.b])
          .size()
          .unstack()
          .reindex(columns=(range(1,df.b.max() + 1)))
          .fillna(0)
          .astype(int)

df1.index.name = None
df1.columns.name = None
print (df1)
   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
</code></pre>

<p>Instead <code>size</code> you can use <code>count</code>, differences are in <a class='doc-link' href="http://stackoverflow.com/documentation/pandas/1822/grouping-data/6874/aggregating-by-size-and-count#t=201609211337167690224">pandas documentation</a>.</p>

python pandas group-by aggregate multiple-columns