简体   繁体   English

熊猫数据框问题。 创建列,其中行单元格获取另一个行单元格的值

[英]Pandas dataframe problem. Create column where a row cell gets the value of another row cell

I have this pandas dataframe.我有这个熊猫数据框。 It is sorted by the "h" column.它按“h”列排序。 What I want is to add two new columns where: The items of each zone, will have a max boundary and a min boundary.我想要的是添加两个新列,其中:每个区域的项目将具有最大边界和最小边界。 (They will be the same for every item in the zone). (它们对于区域中的每个项目都是相同的)。 The max boundary will be the minimum "h" value of the previous zone, and the min boundary will be the maximum "h" value of the next zone最大边界将是前一个区域的最小“h”值,最小边界将是下一个区域的最大“h”值

name    h   w   set row zone
ZZON5   40  36  A   0   0
DWOPN   38  44  A   1   0
5SWYZ   37  22  B   2   0
TFQEP   32  55  B   3   0
OQ33H   26  41  A   4   1
FTJVQ   24  25  B   5   1
F1RK2   20  15  B   6   1
266LT   18  19  A   7   1
HSJ3X   16  24  A   8   2
L754O   12  86  B   9   2
LWHDX   11  68  A   10  2
ZKB2F   9   47  A   11  2
5KJ5L   7   72  B   12  3
CZ7ET   6   23  B   13  3
SDZ1B   2   10  A   14  3
5KWRU   1   59  B   15  3

what i hope for:我希望:

name    h   w   set row zone maxB minB
ZZON5   40  36  A   0   0         26
DWOPN   38  44  A   1   0         26
5SWYZ   37  22  B   2   0         26
TFQEP   32  55  B   3   0         26
OQ33H   26  41  A   4   1    32   16
FTJVQ   24  25  B   5   1    32   16
F1RK2   20  15  B   6   1    32   16
266LT   18  19  A   7   1    32   16
HSJ3X   16  24  A   8   2    18   7
L754O   12  86  B   9   2    18   7
LWHDX   11  68  A   10  2    18   7
ZKB2F   9   47  A   11  2    18   7
5KJ5L   7   72  B   12  3    9  
CZ7ET   6   23  B   13  3    9
SDZ1B   2   10  A   14  3    9
5KWRU   1   59  B   15  3    9  

Any ideas?有任何想法吗?

First group-by zone and find the minimum and maximum of them首先按区域分组并找到其中的最小值和最大值

min_max_zone = df.groupby('zone').agg(min=('h', 'min'), max=('h', 'max'))

Now you can use apply:现在您可以使用申请:

df['maxB'] = df['zone'].apply(lambda x: min_max_zone.loc[x-1, 'min'] 
                               if x-1 in min_max_zone.index else np.nan)

df['minB'] = df['zone'].apply(lambda x: min_max_zone.loc[x+1, 'max']             
                               if x+1 in min_max_zone.index else np.nan)

暂无
暂无

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

相关问题 Pandas 重塑数据框,其中每一行都是单元格值和索引 - Pandas reshape dataframe where each row is cell value and index 在Pandas数据帧中查找任何单元格值= = x,并返回单元格值,列标题,行和相邻单元格值 - Find any cell values >= x in Pandas dataframe and return cell value, column header, row and neighbouring cell value 从 Pandas DataFrame 行获取单元格值 - Get cell value from a pandas DataFrame row python pandas dataframe:操作以获取以另一行的单元格值为条件的每行单元格中的值 - python pandas dataframe: operate to obtain value in cells of each row conditioned to cell value of another row Pandas 单元格值是另一个数据框中的列名 - Pandas cell value is a column name in another dataframe 我如何根据列单元格值和 append 查找一个 dataframe 上的一行到另一个 dataframe 上的一行? - How do i lookup a row on one dataframe based on the column cell value and append that to a row on another dataframe? DataFrame:如何根据行的另一个单元格切换单元格值? - DataFrame: How to toggle a cell value base on another cell of the row? 删除列值<0的Pandas DataFrame行 - Delete Pandas DataFrame row where column value is < 0 熊猫创建复制另一个单元格值的列 - Pandas Create Column that Copies Another Cell Value 熊猫按行号(不是行索引)和列名获取单元格值 - Pandas get cell value by row NUMBER (NOT row index) and column NAME
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM