I tried to find the index which satisfy certain conditions in pandas DataFrame.
For example, we have the following dataframe
and find the index such that
argmin(j) df['A'].iloc[j] >= (df['A'].iloc[i] + 3 ) for all i
so the result will be given by
I finished the work by using for loop, but I believe there is more efficient way to acheieve this job.
Thank you for your reply!
My code is
for i in range(len(df)):
df['B'].iloc[i] = df[df2['A']>= df2['A'].iloc[i]+1].index[0]
but, for loop is too slow for a large data set.
try following method, :)
import pandas as pd
import numpy as np
df = pd.DataFrame({'A': [1,3,5,8,10,12]})
b = pd.DataFrame(df.values - (df['A'].values + 3), index=df.index)
df['B'] = b.where(b >= 0).idxmin()
df