简体   繁体   中英

Python Pandas check cell value in column based on value in another cell

I need to check for every row in a dataframe if the value in a certain column is above 0 or not.

tshirt  pants   sweater socks   Product_1   Product_2   Product_3  Expected
0       1        0       1      sweater      tshirt     pants        True
1       1        0       1      sweater      tshirt     socks        True
0       1        0       0      socks        sweater    socks        False
1       1        0       1      sweater      tshirt     sweater      True
0       0        0       0      socks        sweater    tshirt       False

So for example the value in column 'Product_1' is 'tshirt', I need to check the thshirt column if the value is above 0 or not.

If the value is above 0 for one of the values in the three 'Product' columns, another column could say True, else False (see Expected column)

Code to produce sample data:

import pandas as pd
import numpy as np

recomendations = ['tshirt', 'pants', 'sweater', 'socks']
size = 100


data = pd.DataFrame()

# Generate data
for idx, i in enumerate(recomendations):
    data[i] = np.random.choice([0,1], size=100)

    if idx <= 3:
        data[f'Product_{idx}'] = np.random.choice(recomendations, size=size)
# Sort        
data[recomendations + ['Product_1', 'Product_2', 'Product_3']]

So far i have computed a percentage of True value in a very slow way by looping over the frame:

track = []
no_purchase = 0

cols = list(frame.columns)
str_cols = ['Product_1', 'Product_2', 'Product_3']



for idx, val in frame[column].iteritems():

    if  frame.iloc[idx, cols.index(val)] > 0:
        track.append(1)

    else:
        track.append(0)
        if frame.loc[idx, [i for i in frame.columns if i not in str_cols]].sum() < 1:
                  no_purchase += 1


result = no_purchase / (len(track) - np.sum(track))

return result

Use get_dummies for Product columns with max for indicator output (aways 1,0):

df = pd.get_dummies(data.filter(like='Product'),prefix_sep='',prefix='').max(level=0,axis=1)
print (df)
   socks  sweater  tshirt  pants
0      0        1       1      1
1      1        1       1      0
2      1        1       0      0
3      0        1       1      0
4      1        1       1      0

Then chain conditions for compare values per rows filled by 1 by & for bitwise AND with greater values like 0 :

mask = df.eq(1) & data.loc[:, data.columns.isin(df.columns)].gt(0)
print (mask)
   pants  socks  sweater  tshirt
0   True  False    False   False
1  False   True    False    True
2  False  False    False   False
3  False  False    False    True
4  False  False    False   False

And last test if at least one True per row by DataFrame.any :

data['Expected1'] = mask.any(axis=1)
print (data)
   tshirt  pants  sweater  socks Product_1 Product_2 Product_3  Expected  \
0       0      1        0      1   sweater    tshirt     pants      True   
1       1      1        0      1   sweater    tshirt     socks      True   
2       0      1        0      0     socks   sweater     socks     False   
3       1      1        0      1   sweater    tshirt   sweater      True   
4       0      0        0      0     socks   sweater    tshirt     False   

   Expected1  
0       True  
1       True  
2      False  
3       True  
4      False  

Another approach would be this one

expected = []

for index, row in data.iterrows():
    product1 = row["Product_1"]
    product2 = row["Product_2"]
    product3 = row["Product_3"]
    if row[product1] > 0 or row[product2] > 0 or row[product3] > 0:
        expected.append(True)
    else:
        expected.append(False)

data['Expected'] = expected
print(data)

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