is my code
import pandas as pd
columns1 = ['Student ID', 'Course ID', 'Marks']
data1 = [(1, 10, 100), (2, 400, 200), (3, 30, 300), (3, 30, 300), (3, 30, 300), (3, 30, 300), (3, 30, 300), (3, 30, 300)]
df1 = pd.DataFrame(data1, columns=columns1)
| Student ID | Course ID | Marks |
|---|---|---|
| 1 | 10 | 100 |
| 2 | 400 | 200 |
| 3 | 30 | 300 |
| 3 | 30 | 300 |
| 3 | 30 | 300 |
| 3 | 30 | 300 |
| 3 | 30 | 300 |
| 3 | 30 | 300 |
df1['s'] = np.where((df1['Course ID'] > df1['Marks']) == True, df1['Student ID'], df1['s'].shift(1))
df1
| Student ID | Course ID | Marks | s |
|---|---|---|---|
| 1 | 10 | 100 | NaN |
| 2 | 400 | 200 | 2 |
| 3 | 30 | 300 | 2 |
| 3 | 30 | 300 | NaN |
| 3 | 30 | 300 | NaN |
| 3 | 30 | 300 | NaN |
| 3 | 30 | 300 | NaN |
| 3 | 30 | 300 | NaN |
As you can see, only the information of two rows has changed and the rest are null. This is the result I expect because after column 2 condition "df1['Course ID'] > df1['Marks']" is true
| Student ID | Course ID | Marks | s |
|---|---|---|---|
| 1 | 10 | 100 | NaN |
| 2 | 400 | 200 | 2 |
| 3 | 30 | 300 | 2 |
| 3 | 30 | 300 | 2 |
| 3 | 30 | 300 | 2 |
| 3 | 30 | 300 | 2 |
| 3 | 30 | 300 | 2 |
| 3 | 30 | 300 | 2 |
Thank you for your help
df1['s'].shift(1)you are shifting a column ('s') that does not yet exist, so there is no way that the second image represents the result of running this exact code. You would receive an error. Please update your post with the expected output (as text, not as an image), and explain what logic you are trying to implement.df1['s'] = df1['Student ID'].where(df1['Course ID'] > df1['Marks']).ffill(). Chain.astype('Int64')if you want nullable integers.