I have pandas DataFrame with column:
index, min_or_max
0, np.nan
1, np.nan
2, min
3, np.nan
4, np.nan
5, max
6, np.nan
7, np.nan
8, np.nan
9, max
10, np.nan
11, np.nan
12, min
I want to create col2
such that:
index, min_or_max, col2
0, np.nan np.nan
1, np.nan np.nan
2, min min
3, np.nan np.nan
4, np.nan np.nan
5, max np.nan
6, np.nan np.nan
7, np.nan np.nan
8, np.nan np.nan
9, max max
10, np.nan np.nan
11, np.nan np.nan
12, min min
how can i check for consequetive values in the column and take the last one?
- I can have multiple consequetive value max or min but repetition is max 10 in a row
- I can have repetitions of either in or max value
EDIT:
I tried this:
df1 = df[df['min_or_max'].ne(df['min_or_max'].shift(-1))]
df1["col2"] = df1["min_or_max"]
df1 = df1.reset_index()
df1 = df1[["index", "col2"]]
df = df.reset_index()
df = df[["index"]]
df = df.merge(df1, on="index", how="left")
EDIT:
my proposed solution:
df1 = df.dropna(subset=['min_or_max'], how='all', inplace=False)
df1 = df1[df1['min_or_max'].ne(df1['min_or_max'].shift(-1))]
df = df.reset_index()
df = df[["index", "min_or_max"]]
df1 = df1.reset_index()
df.columns = ["index", "col2"]
df1 = df1[["index", "col2"]]
df = df.merge(df1, on="index", how="left")
source https://stackoverflow.com/questions/73436980/pandas-take-last-consequetive-value
Comments
Post a Comment