嗨我有这样的df,
Name sl no details score
0 Ram 1 ram is going to ooty NaN
1 Ram 2 ram sings well 1.5
2 Ravi 1 ravi play cricket 1.0
3 Ravi 2 ravi is in chennai NaN
4 Kumar 1 kumar passed the exam NaN
5 Kumar 2 kumar is in town NaN
6 Kumar 3 he left 3.0
我正在尝试更改分数列中的值。值应移至df[sl no]==1 or df[Name] is the first occurence of a name
我的预期输出应该是,
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
请帮忙。
答案 0 :(得分:1)
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
答案 1 :(得分:1)
您可以尝试:
df['score'] = (df['score'].replace('',np.nan).groupby(df['Name']).transform(lambda x: x.bfill().ffill()))
df.loc[df['sl no'] != 1, 'score'] = np.NaN
首先使用相同的值填充score
列:
Name sl no details score
0 Ram 1 ram is going to ooty 1.5
1 Ram 2 ram sings well 1.5
2 Ravi 1 ravi play cricket 1.0
3 Ravi 2 ravi is in chennai 1.0
4 Kumar 1 kumar passed the exam 3.0
5 Kumar 2 kumar is in town 3.0
6 Kumar 3 he left 3.0
然后删除列sl no
不是1
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