初始化boolean矩阵,从熊猫一列创建布尔矩阵

本文介绍了一种使用Python和Pandas库来处理数据集的方法。具体来说,是从一个包含多个列的数据集中创建矩阵,并通过比较同一行中不同列的值来填充矩阵。如果两列中的值相同,则矩阵中的相应位置赋值为1;否则赋值为0。此过程将针对六列重复进行,最终将这些矩阵合并成一个汇总矩阵。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

I have been searching for an answer but I don't know what to search for so I'll ask here instead. I'm a beginner python and pandas enthusiast.

I have a dataset where i would like to produce a matrix from a column. The matrix should have the value of 1 if the value in the column and its transposed state is equal and 0 if its not.

input:

id x1

A 1

B 3

C 1

D 5

output:

A B C D

A 1 0 1 0

B 0 1 0 0

C 1 0 1 0

D 0 0 0 1

I would like to do this for six different columns and add the resulting matrixes into one matrix where the values range from 0-6 instead of just 0-1.

解决方案

Partly because there's as of yet no convenient cartesian join (whistles and looks away), I tend to drop down to numpy level and use broadcasting when I need to do things like this. IOW, because we can do things like this

>>> df.x1.values - df.x1.values[:,None]

array([[ 0, 2, 0, 4],

[-2, 0, -2, 2],

[ 0, 2, 0, 4],

[-4, -2, -4, 0]])

We can do

>>> pdf = pd.DataFrame(index=df.id.values, columns=df.id.values,

data=(df.x1.values == df.x1.values[:,None]).astype(int))

>>> pdf

A B C D

A 1 0 1 0

B 0 1 0 0

C 1 0 1 0

D 0 0 0 1

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值