[英]Panda's : Matrix from pd.crosstab()
I'm progressively learning pandas, I figured out that pd.crosstab()
can do marvels but I've hard time to make it work in that case. 我正在逐步学习熊猫,我发现pd.crosstab()
可以pd.crosstab()
惊叹,但在这种情况下我很难使它工作。
I have a list of objects obj
tagged with an int, I want to have the matrix of the objects sharing the same tag (1 if it's the same, 0 else) 我有一个标有int的obj
对象列表,我希望共享相同标签的对象矩阵(如果相同则为1,否则为0)
| obj | tag |
|-----|-----|
| a | 0 |
| b | 2 |
| c | 1 |
| ... | ... |
| z | 2 |
-> ->
| | a | b | c | ... | z |
|-----|---|---|---|-----|---|
| a | 1 | 0 | 0 | . | 0 |
| b | 0 | 1 | 0 | . | 1 |
| c | 0 | 0 | 1 | . | 0 |
| ... | . | . | . | . | 0 |
| z | 0 | 1 | 0 | 0 | 1 |
There are some for
midables ways to do it, is there one more panda-friendly ? 有一些for
中型企业的方式,还有另一种对熊猫友好的方式吗?
PS : Tried with pd.crosstab(df.obj, df.obj, values=df.tag, aggfunc=[np.sum])
but NaN filled. PS:尝试使用pd.crosstab(df.obj, df.obj, values=df.tag, aggfunc=[np.sum])
但使用NaN填充。
You can use merge
with crosstab
and DataFrame.rename_axis
: 您可以merge
与crosstab
和DataFrame.rename_axis
:
df = df.merge(df, on='tag')
df = pd.crosstab(df.obj_x, df.obj_y).rename_axis(None).rename_axis(None, axis=1)
print (df)
a b c z
a 1 0 0 0
b 0 1 0 1
c 0 0 1 0
z 0 1 0 1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.