Rolling Record Of First Time Changes (& Diffs) Between Two Columns In A Df
My data is stored in a pandas dataframe. Rule: _Store the first value that appears in either col1 or col2. (Here it's df['col2'][0]). _ Increment the row, if the next number appea
Solution 1:
This might be of help
df['col'] = np.where(df.col1.isnull(), 'col2', 'col1')
df['group'] = (df.col != df.col.shift(1)).astype('int').cumsum()
df['value'] = np.where(df.col1.isnull(), df['col2'], df['col1'])
df['first'] = df.value.groupby(df.group).transform(lambda s: s.iget(0))
df
col1 col2 col group value first
0 NaN 46.8400 col2 1 46.8400 46.8400
1 NaN 46.8400 col2 1 46.8400 46.8400
2 NaN 46.8400 col2 1 46.8400 46.8400
3 NaN 46.8400 col2 1 46.8400 46.8400
4 44.9501 NaN col1 2 44.9501 44.9501
5 44.9731 NaN col1 2 44.9731 44.9501
6 45.0229 NaN col1 2 45.0229 44.9501
7 45.0480 NaN col1 2 45.0480 44.9501
8 45.0753 NaN col1 2 45.0753 44.9501
9 NaN 45.0753 col2 3 45.0753 45.0753
10 NaN 45.0753 col2 3 45.0753 45.0753
df['first'].diff(1).groupby(df.group).first()
group
1 NaN
2 -1.8899
3 0.1252
Post a Comment for "Rolling Record Of First Time Changes (& Diffs) Between Two Columns In A Df"