'Compare column name with a row value and getting other row value

I have a dataframe like this:

    request_created_at      sponsor_tier    is_active   status  cash_in  2019/10    ...  2021/07    
0   2019/10 2019/10           2.0           True        1       8901.00             ...                                     
1   2019/10 2019/10           2.0           True        2       7602.00             ... 

I want to compare all my columns with date e.g "2019/10" with the values of my first column and check if status == 1 and if status == 1 I want to copy my cash in value into the columns "2019/10" rows



Solution 1:[1]

def check_status (row, data):
    if (row.status == 1) & (row.request_created_at == data):
        return row.total_cash_in
    elif (row.status == 4) & (row.request_created_at == data):
        return row.total_cash_in
    elif (row.status == 7) & (row.request_created_at == data):
        return row.total_cash_in
    else:
        return 0

and then

for data in array_datas:
    df[data] = df.apply(lambda row: check_status(row,data), axis=1)

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 h1tom1