Reputation: 647
I created a binary matrix from 2 pandas columns
df:
ID_2 ID_1
1111 1
22222 2
33333 3
33333 4
44444 5
55555 6
55555 7
66666 8
66666 9
77777 10
77777 11
77777 12
Using:
A = pd.get_dummies(df.set_index('ID_1')['ID_2'].astype(str)).max(level=0)
print (A)
This creates a matrix:
22222 33333 44444 55555 66666 77777 11111
ID_2
1 0 0 0 0 0 0 1
2 1 0 0 0 0 0 0
3 0 1 0 0 0 0 0
4 0 1 0 0 0 0 0
5 0 0 1 0 0 0 0
....
all fine - except the first unique value from ID_1 is placed in the last column. I need the order of the values to be to be retained as in ID_2.
Upvotes: 1
Views: 68
Reputation: 12417
If you want to reorder the columns, I think you need this:
A = A.reindex_axis(['11111'] + list(A.columns[:-1]), axis=1)
You can do in this way:
from collections import OrderedDict
cols = list(OrderedDict.fromkeys(list(df['ID_2'].values)))
cols = [str(i) for i in cols]
A = A.reindex_axis(cols, axis=1)
here you keep the element of the column in a ordered way(and without duplicates) and then you use them as header
Upvotes: 1