在 pandas .loc[] 赋值中访问下一行、上一行或当前行

在 pandas 文档说明书的 if-then 部分,我们可以根据使用loc[].


 df = pd.DataFrame({'AAA' : [4,5,6,7], 

'BBB' : [10,20,30,40],

'CCC' : [100,50,-30,-50]})

#    AAA  BBB  CCC

# 0    4   10  100

# 1    5   20   50

# 2    6   30  -30

# 3    7   40  -50


df.loc[df.AAA >= 5,'BBB'] = -1

#    AAA  BBB  CCC

# 0    4   10  100

# 1    5   -1   50

# 2    6   -1  -30

# 3    7   -1  -50

但是,如果我想使用 编写涉及前一行或后一行的条件.loc[]怎么办?例如,假设我想分配当前行和下一行df.BBB=5之间的差值大于或等于 50 的任何位置。然后我想创建一个条件,该条件为我提供以下数据框:df.CCCdf.CCC


#    AAA  BBB  CCC

# 0    4    5  100 <-| 100 - 50 = 50, assign df.BBB = 5

# 1    5    5   50 <-| 50 -(-30)= 80, assign df.BBB = 5 

# 2    6   -1  -30 <-| 30 -(-50)= 20, don't assign df.BBB = 5

# 3    7   -1  -50 <-| (-50) -0 =-50, don't assign df.BBB = 5

我怎样才能得到这个结果?


编辑 我希望找到的答案是这样的


mask = df['CCC'].current - df['CCC'].next >= 50

df.loc[mask, 'BBB'] = 5

因为我对如何访问数据帧中正在考虑的当前行上方或下方的值的一般问题感兴趣。(不一定要解决这个玩具示例。)


diff() 将适用于我首先描述的示例,但是其他情况呢,例如,我们想要比较两个元素而不是减去它们?


如果我采用前一个数据框并且我想找到当前列条目与下一个条目不匹配的所有行,df.BBB然后df.CCC根据这些比较进行分配,该怎么办?


if df.BBB.current == df.CCC.next:

    df.CCC = 1


#    AAA  BBB  CCC     

# 0    4    5    1 <-|  5 ==  5, assign df.CCC = 1

# 1    5    5   50 <-|  5 != -1, do nothing

# 2    6   -1    1 <-| -1 == -1, assign df.CCC = 1

# 3    7   -1  -50 <-| -1 !=  0, do nothing

有没有办法用熊猫来做到这一点.loc[]?


慕标5832272
浏览 569回答 2
2回答

慕丝7291255

给定的>>> df&nbsp; &nbsp;AAA&nbsp; BBB&nbsp; CCC0&nbsp; &nbsp; 4&nbsp; &nbsp;10&nbsp; 1001&nbsp; &nbsp; 5&nbsp; &nbsp;20&nbsp; &nbsp;502&nbsp; &nbsp; 6&nbsp; &nbsp;30&nbsp; -303&nbsp; &nbsp; 7&nbsp; &nbsp;40&nbsp; -50您可以首先通过计算布尔掩码>>> mask = df['CCC'].diff(-1) >= 50>>> mask0&nbsp; &nbsp; &nbsp;True1&nbsp; &nbsp; &nbsp;True2&nbsp; &nbsp; False3&nbsp; &nbsp; FalseName: CCC, dtype: bool然后发出>>> df.loc[mask, 'BBB'] = 5>>>&nbsp;>>> df&nbsp; &nbsp;AAA&nbsp; BBB&nbsp; CCC0&nbsp; &nbsp; 4&nbsp; &nbsp; 5&nbsp; 1001&nbsp; &nbsp; 5&nbsp; &nbsp; 5&nbsp; &nbsp;502&nbsp; &nbsp; 6&nbsp; &nbsp;30&nbsp; -303&nbsp; &nbsp; 7&nbsp; &nbsp;40&nbsp; -50更一般地说,你可以计算一个班次>>> df['CCC_next'] = df['CCC'].shift(-1) # or df['CCC'].shift(-1).fillna(0)>>> df&nbsp; &nbsp;AAA&nbsp; BBB&nbsp; CCC&nbsp; CCC_next0&nbsp; &nbsp; 4&nbsp; &nbsp; 5&nbsp; 100&nbsp; &nbsp; &nbsp; 50.01&nbsp; &nbsp; 5&nbsp; &nbsp; 5&nbsp; &nbsp;50&nbsp; &nbsp; &nbsp;-30.02&nbsp; &nbsp; 6&nbsp; &nbsp;30&nbsp; -30&nbsp; &nbsp; &nbsp;-50.03&nbsp; &nbsp; 7&nbsp; &nbsp;40&nbsp; -50&nbsp; &nbsp; &nbsp; &nbsp;NaN...然后做任何你想做的事,例如:>>> df['CCC'].sub(df['CCC_next'], fill_value=0)0&nbsp; &nbsp; 50.01&nbsp; &nbsp; 80.02&nbsp; &nbsp; 20.03&nbsp; &nbsp;-50.0dtype: float64>>> mask = df['CCC'].sub(df['CCC_next'], fill_value=0) >= 50>>> mask0&nbsp; &nbsp; &nbsp;True1&nbsp; &nbsp; &nbsp;True2&nbsp; &nbsp; False3&nbsp; &nbsp; Falsedtype: bool尽管对于您问题中的特定问题,该diff方法就足够了。

蓝山帝景

您可以使用 enumerate 函数同时访问行及其索引。因此,您可以根据当前行的索引获取上一行和下一行。我在下面提供了一个示例脚本供您参考:import pandas as pddf = pd.DataFrame({'AAA' : [4,5,6,7],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'BBB' : [10,20,30,40],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'CCC' : [100,50,-30,-50]}, index=['a','b','c','d'])print('row_pre','row_pre_AAA','row','row_AA','row_next','row_next_AA')for irow, row in enumerate(df.index):&nbsp; &nbsp; if irow==0:&nbsp; &nbsp; &nbsp; &nbsp; row_next = df.index[irow+1]&nbsp; &nbsp; &nbsp; &nbsp; print('row_pre', "df.loc[row_pre,'AAA']", row, df.loc[row,'AAA'], row_next, df.loc[row_next,'AAA'])&nbsp; &nbsp; elif irow>0 and irow<df.index.size-1:&nbsp; &nbsp; &nbsp; &nbsp; row_pre = df.index[irow-1]&nbsp; &nbsp; &nbsp; &nbsp; row_next = df.index[irow+1]&nbsp; &nbsp; &nbsp; &nbsp; print(row_pre, df.loc[row_pre,'AAA'], row, df.loc[row,'AAA'], row_next, df.loc[row_next,'AAA'])&nbsp; &nbsp; else:&nbsp; &nbsp; &nbsp; &nbsp; row_pre = df.index[irow-1]&nbsp; &nbsp; &nbsp; &nbsp; print(row_pre, df.loc[row_pre,'AAA'], row, df.loc[row,'AAA'], 'row_next', "df.loc[row_next,'AAA']")输出如下:row_pre row_pre_AAA row row_AA row_next row_next_AArow_pre df.loc[row_pre,'AAA'] a 4 b 5a 4 b 5 c 6b 5 c 6 d 7c 6 d 7 row_next df.loc[row_next,'AAA']
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Python