我有一个概念,我需要做什么,但我不能写正确的代码运行,请看一看,给予一些建议。
步骤1.查找第二列中包含值的行
步骤2.对于这些行,将第一列中的值与它们的前一行进行比较
步骤3.删除第一列值较大的行
|missing | diff |
|--------|------|
| 0 | nan |
| 1 | 60 |
| 1 | nan |
| 0 | nan |
| 0 | nan |
| 1 | 180 |
| 1 | nan |
| 0 | 120 |
例如,我想将缺失的值与diff [120,180,60]中的行值及其之前的行进行比较。最后,欲望的框架看起来就像
|missing | diff |
|--------|------|
| 0 | nan |
| 1 | nan |
| 0 | nan |
| 0 | nan |
| 0 | 120 |
根据答案更新问题,得到与原始DF相同的DF
import pandas as pd
import numpy as np
data={'missing':[0,1,1,0,0,1,1,0],'diff':[np.nan,60,np.nan,np.nan,np.nan,180,np.nan,120]}
df=pd.DataFrame(data)
df
missing diff
0 0 NaN
1 1 60.0
2 1 NaN
3 0 NaN
4 0 NaN
5 1 180.0
6 1 NaN
7 0 120.0
if df['diff'][ind]!=np.nan:
if ind!=0:
if df['missing'][ind]>df['missing'][ind-1]:
df=df.drop(ind,0)
else:
df=df.drop(ind-1,0)
df
missing diff
0 0 NaN
1 1 60.0
2 1 NaN
3 0 NaN
4 0 NaN
5 1 180.0
6 1 NaN
7 0 120.0
4条答案
按热度按时间fnatzsnv1#
IIUC,您可以尝试:
输出值:
qcuzuvrc2#
这将是肯定的工作:
5w9g7ksd3#
这将工作
对于df中的ind.index:
6uxekuva4#
import pandas as pd #import pandas
define字典
data='missing':[0,1,1,0,0,1,1,0],'diff ':[nan,60,nan,nan,180,nan,120]}
dictionary to语法
df=pd.DataFrame(data)
打印(df)
对于嵌套框中的每行
对于df中的ind.index:
打印(df)