-1

I tried to find the index which satisfy certain conditions in pandas DataFrame.

For example, we have the following dataframe

enter image description here

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

enter image description here

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.

bhansa
  • 7,282
  • 3
  • 30
  • 55
user155214
  • 115
  • 1
  • 5
  • 2
    Welcome to StackOverflow. Please don't post links to screenshots of data - just include example data inline, instead. That makes it easier for people to help you, and it protects against the possibility of links becoming dead in the future. – andrew_reece Dec 13 '17 at 05:02
  • 2
    create minimal working example so everyone could run it and make modifications. – furas Dec 13 '17 at 05:29

1 Answers1

0

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
zyun
  • 49
  • 3