I have to find within a dataframe when a user registered or unregistered, for this I use the following code. Understanding that when the user appears for the first time it was a high and the inverse for the low of the same.
from curses import erasechar
import pandas as pd
df = pd.DataFrame({'Fecha': ['2022-08-29', '2022-08-30','2022-09-03', '2022-09-04','2022-09-05','2022-09-12'],'id': [20076,20076,20076,20076,20076,20076]})
df['Fecha'] = pd.to_datetime(df['Fecha'])
df = (df.set_index('Fecha')
.reindex(pd.date_range('2022-08-29', '2022-10-01', freq='D'))
.rename_axis(['Fecha'])
.fillna(0)
.reset_index())
pd.set_option('display.max_rows', None)
print(df)
The result is as follows:
My query is how I can get to this final result, showing only the data of when he was discharged and the same for discharge but not repeating day by day, since it is understood that only the date on which he was discharged is sought and the low. Having as a final result according to the example above the following output.
a pleasure to greet
Here I show you how I approached the result. First, from the main frame, I create an empty obj type column to dump the final HIGH and LOW results there, filtering only on the occurrence of each date because the other dates are empty
this throws me a dataframe of this type
Note that the indices of that filtered frame is [0,1,5,6,7,14], but for logic purposes we will reset those indices to [0,1,2,3,4,5]
Understanding that according to the second image where the results table appears, the highest occurrence DATE would be a "HIGH", then I iterate with the parity or oddness of the index, logically the EVEN are HIGH and the ODD are LOW
The previous FOR would be the logic that will return the high and low in the RESULT column
I hope I have contributed to the solution. Greetings