简体   繁体   中英

Pandas - creating 2 new columns based on 2 columns and a separate test column

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

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.

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.

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.

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']]

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

For example :

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

In your case you can do

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM