I have tried several things, but my pandas knowledge is a bit lost and I can’t find a solution.
There is a DataFrame that contains several columns (44) with the information whether buttons have been pressed or not, represented by 4 states. I am trying to get the information in which row it was pressed. Maybe there is a performant solution. The data frame has a length of almost 4 million rows.
Example:
Button 1 | Button 2 | Button 3 |
---|---|---|
0 | 1 | 2 |
0 | 1 | 0 |
0 | 0 | 2 |
1 | 0 | 0 |
0 | 1 | 0 |
A button is pressed when the value changes from zero to one:
Button 1 | Button 2 | Button 3 |
---|---|---|
pressed | ||
pressed |
I tried it with apply and this function:
def if_pressed(new, old):
if new == 1 and old == 0:
return 1
else:
return 0
This is not working due to swith() and apply are not working together. With apply I cannot switch the row:
columns = list(df)
for c in columns:
df[c + '_comp'] = df.apply(lambda row:if_pressed(row[c], row[c].shift()), axis=1)
I also tried to copy and shift the data frame. Here it was only possible for me to find different cells, but not the possibility to check the difference.
Maybe someone can please help me.