本文介绍了 pandas 的:来自pd.crosstab()的矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在逐步学习熊猫,我发现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.
我有一个标有int的对象obj
的列表,我希望对象的矩阵共享相同的标签(如果相同,则为1,否则为0)
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)
| 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 |
有一些for
midables方法可以做到,还有熊猫友好型吗?
There are some for
midables ways to do it, is there one more panda-friendly ?
PS:尝试过pd.crosstab(df.obj, df.obj, values=df.tag, aggfunc=[np.sum])
,但填充了NaN.
PS : Tried with pd.crosstab(df.obj, df.obj, values=df.tag, aggfunc=[np.sum])
but NaN filled.
推荐答案
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
这篇关于 pandas 的:来自pd.crosstab()的矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!