使用Pandas跳过特定列的下三行

2024-03-29 01:58:59 发布

您现在位置:Python中文网/ 问答频道 /正文

我一直在研究这个代码: 我想完成以下工作 -如果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

Tags: the代码ingtnonedfforread
1条回答
网友
1楼 · 发布于 2024-03-29 01:58:59

试试这个:

import pandas as pd
df = pd.DataFrame({'a': [1,5,1,2,2,2,1], 'b': [1,4,2,4,8,1,2], 'c': [1,2,6,6,2,1,2]})
for i in df['c']:
   if i>3:
      try:
         idx = df['c'].tolist().index(i)# drop/delete the next 3 rows regardless of the value
         print(idx)
      except:
         pass
      for i in range(idx, idx+3):
         df.at[i, 'c'] = None
print(df)

输出:

   a  b  c
0  1  1  1.0
1  5  4  2.0
2  1  2  NaN
3  2  4  NaN
4  2  8  NaN
5  2  1  1.0
6  1  2  2.0

我的解决方案是使用虚拟数据帧

我得到了项目的索引,如果项目大于3,则通过项目索引的范围迭代到项目索引加3,然后执行at函数将值设置为Nan

在我的编辑我只是添加了尝试和除了,现在它的工作

对于5行:

我认为这个代码是你想要的,我也认为这是更有效的

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:
     for i in range(index+1, index+6):
         df.at[i, 'c'] = None
     print(df['c'])
     break

输出:

0    1.0
1    4.5
2    NaN
3    NaN
4    NaN
5    NaN
6    NaN
7    3.0
8    4.0
9    7.0
Name: c, dtype: float64

相关问题 更多 >