I want to count how many times there is:
- "Increase" to "Increase"
- "Increase" to "Decrease"
- "Increase" to "Unchanged"
- "Decrease" to "Increase"
- "Decrease" to "Decrease"
- "Decrease" to "Unchanged"
- "Unchanged" to "Increase"
- "Unchanged" to "Decrease"
- "Unchanged" to "Unchanged"
Code must be written in python.
CodePudding user response:
I'm creating a sample dataframe to work on this problem.
import pandas as pd
import numpy as np
df = pd.DataFrame({'id': np.arange(0,16), "trend": ['in','de', 'in',
'in','in','un','de','de','un','un','de','de','in','de','in','in']})
1st: Make a list of the column named 'trend'
val = list(df['trend'])
2nd: Create a new list and add each pair of tuples into the list. Each tuple contains the first value and its consecutive next value according to the iteration.
f = val[0]
list_trend = []
for x in val[1:]:
list_trend.append((f,x))
f = x
The output list will be like this