我一直在研究这个代码: 我想完成以下工作 -如果Pout>;3,则删除接下来的3行
df=pd.read_csv(file,sep=',',usecols=['Iin', 'Iout','Pout'])
print(df['Pout'])
for i in df['Pout']:
if i>3:
df.drop(df[3:])# drop/delete the next 3 rows regardless of the value
print(df)
任何帮助都将不胜感激
谢谢
我是根据你的第一个密码编出来的。但是你刚刚发布的更新版本更有效。在满足条件后,我现在要放弃接下来的五排。你知道吗
import pandas as pd
df = pd.DataFrame({'a': [1,5.0,1,2.3,2.1,2,1,3,4,7], 'b':
[1,4,0.2,4.5,8.2,1,2,3,4,7], 'c': [1,4.5,5.4,6,2,4,2,3,4,7]})
for index in range(len(df['c'])):
if df['c'][index] >3:
df.at[index+1, 'c'] = None
df.at[index+2, 'c'] = None
df.at[index+3, 'c'] = None
df.at[index+4, 'c'] = None
df.at[index+5, 'c'] = None
print(df['c'])
break
试试这个:
输出:
我的解决方案是使用虚拟数据帧
我得到了项目的索引,如果项目大于3,则通过项目索引的范围迭代到项目索引加3,然后执行
at
函数将值设置为Nan
在我的编辑我只是添加了尝试和除了,现在它的工作
对于5行:
我认为这个代码是你想要的,我也认为这是更有效的
输出:
相关问题 更多 >
编程相关推荐