python 多条件判断 生成新列_python pandas根据其他列中的条件进行新列分类

在给定的pandas DataFrame中,通过交易ID分组并检查产品类别,创建了一个新的列'transaction_category'。当交易ID包含唯一的产品类别时,新列值为'类别 only',否则为所有类别用' & '连接。
摘要由CSDN通过智能技术生成

Working with the following python pandas dataframe df:

df = pd.DataFrame({'transaction_id': ['A123','A123','B345','B345','C567','C567','D678','D678'],

'product_id': [255472, 251235, 253764,257344,221577,209809,223551,290678],

'product_category': ['X','X','Y','Y','X','Y','Y','X']})

transaction_id | product_id | product_category

A123 255472 X

A123 251235 X

B345 253764 Y

B345 257344 Y

C567 221577 X

C567 209809 Y

D678 223551 Y

D678 290678 X

I need to add another column "transaction_category", which looks at the transaction_id and which product categories are in the transaction_id.

This is the output I am looking for:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值