0

I encountered a problem in create a sparse indicator matrix.

Let me describe my problem in brief.

Suppose I have a list of categories:

cate = ["a", "b", "c", "d", "e"]

a matrix in each of whose row contains a subset of category:

mat = [["a","c"],["b"],["c","d","e"]]

I want to get a sparse indicator matrix(could be a DataFrame):

result = [[1,0,1,0,0],[0,1,0,0,0],[0,0,1,1,1]]

If it could be derived by pandas operations, that could never be more cool!

Jacy Xi
  • 11
  • 2
  • Use `mlb = MultiLabelBinarizer() pd.DataFrame(mlb.fit_transform(mat),columns=mlb.classes_)` – jezrael Oct 04 '20 at 04:59
  • If I want to do more based on the operation above, that is, I want to add order in it. For example, result = [[1,0,2,0,0],[0,1,0,0,0],[0,0,1,2,3]]. Could I handle this? – Jacy Xi Oct 04 '20 at 07:34

0 Answers0