我尝试按条件转换行数:
df.loc[df['file'] == '0.mp4','xmin'] = df['xmin']/4
df.loc[df['file'] == '2.mp4','xmin'] = df['xmin']/4
df.loc[df['file'] == '8.mp4','xmin'] = df['xmin']/4
df.loc[df['file'] == '10.mp4','xmin'] = df['xmin']/4
是否可以在一行中执行此操作?
这吗?
df.loc[df['file'] == '0.mp4','xmin'] = df['xmin']/4
df.loc[df['file'] == '2.mp4','xmin'] = df['xmin']/4
df.loc[df['file'] == '0.mp4','ymin'] = df['ymin']/4
df.loc[df['file'] == '2.mp4','ymin'] = df['ymin']/4
答案 0 :(得分:4)
您可以使用pandas.Series.isin
:
df.loc[df['file'].isin(['0.mp4', '2.mp4', '8.mp4', '10.mp4']), 'xmin'] = df['xmin'] / 4
答案 1 :(得分:1)
将Series.isin
用于list
的支票隶属关系,也可以将= df['xmin']/4
简化为/= 4
:
df.loc[df['file'].isin(['0.mp4','2.mp4','8.mp4','10.mp4']),'xmin'] /= 4