Hana
Hana

Reputation: 1470

How to make some pandas column values default to another value in a different column, but same row?

I have a sample DataFrame that looks like this:

 ID        Product    UPC    Units Sold 
 no link   cereal    3463    12
 2211      cereal    2211    13
 2211      cereal    8900    11
 2211      cereal    6754    14
 no link   cereal    9012    13 
 3340      cereal    3340    12
 3340      cereal    5436    15

The 'ID' column identifies similar products into one product family ID. The ID is created by the first UPC number of that family. 'No link' identifies products that are the only member of their family. What I want is set the 'no link' values to default to the UPC number. This is what I want my output to look like:

 ID        Product    UPC    Units Sold 
 3463      cereal    3463    12
 2211      cereal    2211    13
 2211      cereal    8900    11
 2211      cereal    6754    14
 9012      cereal    9012    13 
 3340      cereal    3340    12
 3340      cereal    5436    15

This is what I have so far:

 for row in product_families:
     if product_families.loc['Product Family Number'] == 'no link':

Upvotes: 1

Views: 49

Answers (2)

Allen Qin
Allen Qin

Reputation: 19957

Scott Boston's solution should work.

Maybe worth trying a different approach using apply row wise.

df['ID']=df.apply(lambda x: x.UPC if x.ID=='no link' else x.ID, axis=1)

Upvotes: 1

Scott Boston
Scott Boston

Reputation: 153460

Use loc with boolean indexing and let Pandas assign with intrinsic data alignment:

df.loc[df.ID.eq('no link'),'ID'] = df.UPC

Output:

     ID Product   UPC  Units Sold
0  3463  cereal  3463          12
1  2211  cereal  2211          13
2  2211  cereal  8900          11
3  2211  cereal  6754          14
4  9012  cereal  9012          13
5  3340  cereal  3340          12
6  3340  cereal  5436          15

Upvotes: 1

Related Questions