3 回答
TA贡献1796条经验 获得超7个赞
# mcve:
# np.random.seed(42)
# df = pd.DataFrame({1: np.random.choice(('asdf', 'Method', 'Replicates', 'StandardDeviation'), 10)})
# 1
# 0 Replicates
# 1 StandardDeviation
# 2 asdf
# 3 Replicates
# 4 Replicates
# 5 StandardDeviation
# 6 asdf
# 7 asdf
# 8 Replicates
# 9 Method
创建一个面具pd.Series.isin()
mask = df[1].isin(('Method', 'Replicates', 'StandardDeviation'))
并用于where更改掩码为的系列的子集False。
df.loc[5:9, 1] = df.loc[5:9, 1].where(mask, 'new better value')
结果:
# 1
# 0 Replicates
# 1 StandardDeviation
# 2 asdf
# 3 Replicates
# 4 Replicates
# 5 StandardDeviation
# 6 new better value
# 7 new better value
# 8 Replicates
# 9 Method
TA贡献1842条经验 获得超12个赞
TA贡献1801条经验 获得超8个赞
试试这个:
df.loc[109:140]["NAME_OF_COLUMN_1"] = df.apply(lambda row: value1
if row['NAME_OF_COLUMN_1'] not in ['Method', 'Replicates', 'StandardDeviation']
else row['NAME_OF_COLUMN_1'], axis=1)
添加回答
举报