next
в понимании списка
Условно вызовите next
на итераторе в понимании списка.
assert df['sl no'].eq(1).sum() == df['score'].notna().sum()
it = iter(df.score.dropna().tolist())
df['score'] = [
next(it) if i else np.nan for i in df['sl no'].eq(1)
]
df
Name sl no details score
0 Ram 1 ram is going to ooty 1.5
1 Ram 2 ram sings well NaN
2 Ravi 1 ravi play cricket 1.0
3 Ravi 2 ravi is in chennai NaN
4 Kumar 1 kumar passed the exam 3.0
5 Kumar 2 kumar is in town NaN
6 Kumar 3 he left NaN
Если ваш assert
утверждение не выполнено, что-то не так с вашими данными, а то, что вы просите, неосуществимо.
loc
На основе назначения
v = df.score.dropna().tolist()
df['score'] = np.nan
df.loc[df['sl no'].eq(1), 'score'] = v
df
Name sl no details score
0 Ram 1 ram is going to ooty 1.5
1 Ram 2 ram sings well NaN
2 Ravi 1 ravi play cricket 1.0
3 Ravi 2 ravi is in chennai NaN
4 Kumar 1 kumar passed the exam 3.0
5 Kumar 2 kumar is in town NaN
6 Kumar 3 he left NaN