我有一个二维numpy数组如下:
[["cat","dog","dog","mouse","man"],
["rhino","rhino","bat","rhino","dino","dino"],
["zebra","alien","alien","alien","alien"]]
我想在每一行上执行numpy.unique以计算每个标签的出现次数,遗憾的是我不认为这是可能的,因为numpy.unique将返回不同长度的向量:
[["cat","dog","mouse","man"]
["rhino","bat","dino"]
["zebra","alien"]]
(similar then for the counts)
所以这显然不会起作用。
有人知道我可以解决这个问题吗?
答案 0 :(得分:1)
试试这个:
a = pd.DataFrame([["cat","dog","dog","mouse","man"],
["rhino","rhino","bat","rhino","dino","dino"],
["zebra","alien","alien","alien","alien"]])
a.apply(lambda x: pd.Series(x.unique()), axis=1)