pandas -基于2列和一个单独的测试列创建2个新列 [英] Pandas - creating 2 new columns based on 2 columns and a separate test column

查看:78
本文介绍了 pandas -基于2列和一个单独的测试列创建2个新列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试根据另一列的值填充2个现有列中的2个新列.

I'm trying to datafill 2 new columns from 2 existing columns, based on the value of another column.

逻辑是,给定正数,买方和卖方应分别从party和cparty字段中填写.如果金额为负,那么情况就相反了,买主是当事方,而不是当事方,而卖主是当事方.

The logic is that given a positive amount the buyer and seller should be filled out from the party and cparty fields respectively. If the amount is negative then the situation is reversed and the buyer is the cparty rather than the party, and the seller is the party.

我正在尝试避免进行迭代-我可以使用下面的表达式获取每个组件,但是,尝试将这些结果与concat,+,+ =,combine_first,fillna和update合并后,我绘制了一个空白如何合并结果.

I'm trying to avoid doing something iterative - I can get each component using the expressions below but, but having tried to concatenate these results with concat, +, +=, combine_first, fillna and update, I've drawn a blank over how to merge the results.

每次它们被覆盖(我怀疑是因为Pandas匹配列名而不是位置),或者我得到2个空列.

Each time they're either been overwritten (I suspect because Pandas matches on the column name, and not position) or I get 2 empty columns.

必须有一种很好的干净的pythonic方式来组合以下内容或类似内容?

There must be a nice clean pythonic way to combine the below, or similar?

df[['Buyer', 'Seller']] = df[df.amount > 0][['party', 'cparty']]
df[['Buyer', 'Seller']] = df[df.amount < 0][['cparty', 'party']]

推荐答案

也许您正在寻找np.where作为一个班轮,即

Maybe you are looking for np.where as a one liner i.e

例如:

df = pd.DataFrame({'key': ['a','b','b','c','c'],'key2': ['a','d','d','e','e'],'key3': ['j','k','l','m','n'], 'x': [1,2,3,4,5]})

df[['new1','new2']] = pd.DataFrame(np.where(df['x']>2,(df['key3'],df['key2']),(df['key2'],df['key3'])).T)

   key key2 key3  x new1 new2
0   a    a    j  1    a    j
1   b    d    k  2    d    k
2   b    d    l  3    l    d
3   c    e    m  4    m    e
4   c    e    n  5    n    e

您可以的话

df[['Buyer', 'Seller']] = pd.DataFrame(np.where(df.amount < 0,(df['cparty'],df['party']),(df['party'],df['cparty'])).T)

这篇关于 pandas -基于2列和一个单独的测试列创建2个新列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆