0

example of dataframeI have a pandas dataframe that contains boolean values. This information is about the users' activity in system. Columns represent the time and rows - users' IDs. True value if user was active in that time and false if he was not. I would like to assign status instead of boolean values. For example, if user was active, status is 1, if he was not active for certain time status is 0, and user left the system - status is -1. Difference between inactive user and who left the system is that for inactive user False followed later by True, while for those who left the system it is always False.

Input looks like:

inputt = pd.DataFrame([[True, False, False, False], [True, False, True, True]])

Expected output is something like:

output = pd.DataFrame([[1, -1, -1, -1], [1, 0, 1, 1]])

I tried to write the function that checks the following column and assign the status based on it as:

def row_to_status(row, columns=range(2,25)):
# -1 = dead, 0 = inactive, 1 = alive
status_row = []
for column in columns:
    status = 1
    if not row[column]:
        status = -1
        next_cols = range(column+1, columns[-1]+1)
        if row[next_cols].sum():
            status = 0
    status_row.append(status)
return status_row

However, it gives me errors about using the range. Range(2,25) is used for the iterating through the columns.

Please help me to solve this problem. Thanks!

0 Answers0