Selecting values from pandas data frame using multiple conditions
<p>I have the following dataframe in Pandas.  Score and Date_of_interest columns are to be calculated. Below it is already filled out to make the explanation of the problem easy.</p>

<p>First let's assume that Score and Date_of_interest columns are filled with NaN's only. Below are the steps to fill the values in them.</p>

<p>a) We are trying to get one date of interest, based on the criteria described below for one PC_id eg. PC_id 200 has 1998-04-10 02:25:00 and so on.</p>

<p>b) To solve this problem we take the PC_id column and check each row to find the change in Item_id, each has a score of 1.  For the same Item_id like in 1st row and second row, has 1 and 1 so the value starts with 1 but does not change in second row.  </p>

<p>c) While moving and calculating the score for the second row it also checks the Datetime difference, if the previous one is more than 24 hours old, it is dropped and score is reset to 1 and cursor moves to third row.</p>

<p>d) When the Score reaches 2, we have reached the qualifying score as in row no 5(index 4) and we copy the corresponding Datetime in Date_of_interest column.</p>

<p>e) We start the new cycle for new PC_id as in row six.</p>

<pre><code>   Datetime        Item_id     PC_id       Value     Score    Date_of_interest

0   1998-04-8 01:00:00   1      200          35         1       NaN
1   1998-04-8 02:00:00   1      200          92         1       NaN
2   1998-04-10 02:00:00  2      200          35         1       NaN
3   1998-04-10 02:15:00  2      200          92         1       NaN
4   1998-04-10 02:25:00  3      200          92         2     1998-04-10 02:25:00

5   1998-04-10 03:00:00  1      201          93         1       NaN
6   1998-04-12 03:30:00  3      201          94         1       NaN
7   1998-04-12 04:00:00  4      201          95         2       NaN
8   1998-04-12 04:00:00  4      201          26         2     1998-04-12 04:00:00
9   1998-04-12 04:30:00  2      201          98         3       NaN

10  1998-04-12 04:50:00  1      202         100         1       NaN
11  1998-04-15 05:00:00  4      202         100         1       NaN
12  1998-04-15 05:15:00  3      202         100         2   1998-04-15 05:15:00
13  1998-04-15 05:30:00  2      202         100         3       NaN
14  1998-04-15 06:00:00  3      202         100         NaN     NaN
15  1998-04-15 06:00:00  3      202         222         NaN     NaN
</code></pre>

<p>Final table should be as follows:</p>

<pre><code>    PC_id      Date_of_interest  

0   200       1998-04-10 02:25:00
1   201       1998-04-12 04:00:00
2   202       1998-04-15 05:15:00
</code></pre>

<p>Thanks for helping.</p>

<p>Update : Code I am working on currently:</p>

<pre><code>df_merged_unique = df_merged['PC_id'].unique()
score = 0

for i, row in df_merged.iterrows():
    for elem in df_merged_unique:
        first_date = row['Datetime']
        first_item = 0
        if row['PC_id'] == elem:
            if row['Score'] &lt; 2:
                if row['Item_id'] != first_item:
                    if row['Datetime']-first_date &lt;= pd.datetime.timedelta(days=1):
                        score += 1
                        row['Score'] = score
                        first_date = row['Datetime']
                    else:
                        pass
                else:
                    pass
            else:
                row['Date_of_interest'] = row['Datetime']
                break
        else:
            pass
</code></pre>