My question is similar to stackoverflow.com/q/7549410
I have paired data which look like this:
ID ATTR
3 10
1 20
1 20
4 30
I want to count the unique pairs and store those frequency counts in a matrix like this:
10 20 30
1 | 0 2 0
3 | 1 0 0
4 | 0 0 1
Alternatively, if it's known that ID takes values in {1, 2, 3, 4} while ATTR in {0, 10, 20, 30} then I want a matrix as such:
0 10 20 30
1 | 0 0 2 0
2 | 0 0 0 0
3 | 0 1 0 0
4 | 0 0 0 1
Question: What's the fastest way to do both of them in Python or NumPy?
I have tried using Pandas but I get an empty DataFrame:
import numpy as np
import pandas as pd
x = pd.DataFrame([[3, 10], [1, 20], [1, 20], [4, 30]])
x.pivot_table(index = 0, columns = 1, fill_value = 0, aggfunc = 'sum')