简体   繁体   English

遍历熊猫行

[英]Iterating over pandas rows

Having a df : df

cell;value
0;8
1;2
2;1
3;6
4;4
5;6
6;7

And i'm trying to define a function that will check the cell values of the row after the observed one. 我正在尝试定义一个函数,该函数将检查观察到的行之后的单元格值。 If the value of the cell after the observed one (i+1) is bigger than then observed (i) , than the values in a new column maxValue is equal to 0, if smaller - 1. 如果在观察到的一个(i+1)之后的单元格值大于然后观察到的(i) ,则新列maxValue值等于0,如果较小则为-1。

The final df should look like: 最终的df应该如下所示:

 cell;value;maxValue
    0;8;1
    1;2;1
    2;1;0
    3;6;1
    4;4;0
    5;6;0
    6;7;0

My solution that does not work yet is: 我尚无法解决的解决方案是:

    def MaxFind(df, a, col='value'):
if df.iloc[a+1][col] > df.iloc[a][col]:
    return 0

df['maxValue'] = df.apply(lambda row: MaxFind(df, row.value), axis=1)

I believe you need shift with comparing by gt , inverting mask and cast to integers: 我相信你需要shift与比较gt ,反向掩码和转换为整数:

df['maxValue'] = (~df['value'].shift().gt(df['value'])).astype(int)
#another solution
#df['maxValue'] = df['value'].shift().le(df['value']).astype(int)
print (df)
   cell  value  maxValue
0     0      8         1
1     1      2         0
2     2      1         0
3     3      6         1
4     4      4         0
5     5      6         1
6     6      7         1

Details: 细节:

df['shifted'] = df['value'].shift()
df['mask'] = (df['value'].shift().gt(df['value']))
df['inverted_mask'] = (~df['value'].shift().gt(df['value']))
df['maxValue'] = (~df['value'].shift().gt(df['value'])).astype(int)
print (df)
   cell  value  shifted   mask  inverted_mask  maxValue
0     0      8      NaN  False           True         1
1     1      2      8.0   True          False         0
2     2      1      2.0   True          False         0
3     3      6      1.0  False           True         1
4     4      4      6.0   True          False         0
5     5      6      4.0  False           True         1
6     6      7      6.0  False           True         1

EDIT: 编辑:

df['maxValue'] = df['value'].shift(1).le(df['value'].shift(-1)).astype(int)

print (df)
   cell  value  maxValue
0     0      8         0
1     1      2         0
2     2      1         1
3     3      6         1
4     4      4         1
5     5      6         1
6     6      7         0

df['shift_1'] = df['value'].shift(1)
df['shift_-1'] = df['value'].shift(-1)
df['mask'] = df['value'].shift(1).le(df['value'].shift(-1))
df['maxValue'] = df['value'].shift(1).le(df['value'].shift(-1)).astype(int)

print (df)
   cell  value  shift_1  shift_-1   mask  maxValue
0     0      8      NaN       2.0  False         0
1     1      2      8.0       1.0  False         0
2     2      1      2.0       6.0   True         1
3     3      6      1.0       4.0   True         1
4     4      4      6.0       6.0   True         1
5     5      6      4.0       7.0   True         1
6     6      7      6.0       NaN  False         0

If shift values, get for first or last ones missing values. 如果是移位值,请获取第一个或最后一个缺少的值。 If necessary, is possible repalce them by first no NaN or last non NaNs with forward or back filling: 如有必要,可以通过先不添加NaN或最后不添加NaN进行正向或反向填充来替换它们:

df['shift_1'] = df['value'].shift(2)
df['shift_-1'] = df['value'].shift(-2)
df['mask'] = df['value'].shift(2).le(df['value'].shift(-2))
df['maxValue'] = df['value'].shift(2).le(df['value'].shift(-2)).astype(int)
print (df)
   cell  value  shift_1  shift_-1   mask  maxValue
0     0      8      NaN       1.0  False         0
1     1      2      NaN       6.0  False         0
2     2      1      8.0       4.0  False         0
3     3      6      2.0       6.0   True         1
4     4      4      1.0       7.0   True         1
5     5      6      6.0       NaN  False         0
6     6      7      4.0       NaN  False         0

df['shift_1'] = df['value'].shift(2).bfill()
df['shift_-1'] = df['value'].shift(-2).ffill()
df['mask'] = df['value'].shift(2).bfill().le(df['value'].shift(-2).ffill())
df['maxValue'] = df['value'].shift(2).bfill().le(df['value'].shift(-2).ffill()).astype(int)

print (df)
   cell  value  shift_1  shift_-1   mask  maxValue
0     0      8      8.0       1.0  False         0
1     1      2      8.0       6.0  False         0
2     2      1      8.0       4.0  False         0
3     3      6      2.0       6.0   True         1
4     4      4      1.0       7.0   True         1
5     5      6      6.0       7.0   True         1
6     6      7      4.0       7.0   True         1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM