简体   繁体   English

在Pandas中,如何根据另一行中的另一列值更新一行中的列值

[英]In Pandas how to update column value in one row based on another column value in another row

I want to update value in a cell based on value in another cell in different row. 我想根据不同行中另一个单元格中的值更新单元格中的值。

My dataframe is given below: 我的数据框如下:

OrderId   OrderType   Exch
    P_001       P         NYSE
    P_001_1     C          | | 
    P_002       P          |CHIX|
    P_002_1     C          | |
    P_002_2     C          | |

And i want the result to be 我希望结果如此

OrderId   OrderType    Exch

P_001       P          |NYSE|
P_001_1     C          |NYSE|
P_002       P          |CHIX|
P_002_1     C          |CHIX|
P_002_2     C          |CHIX|

Using .loc i can update same rows but i am not able to find any solution in Pandas data-frame for such an update. 使用.loc我可以更新相同的行,但我无法在Pandas数据框中找到任何解决方案来进行此类更新。

While i ask this question, i am try to split the Order id and search in the data frame to update the Exch values. 当我问这个问题时,我试图拆分订单ID并在数据框中搜索以更新Exch值。

If not exist values are missing, use forward filling missing values: 如果不存在值,则使用向前填充缺失值:

df['Exch'] = df['Exch'].ffill()

Or use Series.str.split for new DataFrame , groupby by first and second column with GroupBy.transform and GroupBy.first : 或者使用Series.str.split作为新的DataFrame ,使用GroupBy.transformGroupBy.first第一和第二列GroupBy.first

df1 = df['OrderId'].str.split('_', expand=True)
df['Exch'] = df.groupby([df1[0], df1[1]])['Exch'].transform('first')

print (df)
   OrderId OrderType  Exch
0    P_001         P  NYSE
1  P_001_1         C  NYSE
2    P_002         P  CHIX
3  P_002_1         C  CHIX
4  P_002_2         C  CHIX

Another idea is get rows with P , create Series and map : 另一个想法是获取P行,创建系列和map

s = df[df['OrderType'].eq('P')].set_index('OrderId')['Exch']
df['Exch'] = df['OrderId'].str.rsplit('_', n=1).str[0].map(s).fillna(df['Exch'])
print (df)
   OrderId OrderType  Exch
0    P_001         P  NYSE
1  P_001_1         C  NYSE
2    P_002         P  CHIX
3  P_002_1         C  CHIX
4  P_002_2         C  CHIX
df= df.ffill(axis = 0) 
print(df)

         a  b     c
0    P_001  P  NYSE
1  P_001_1  C  None
2    P_002  P  CHIX
3  P_002_1  C  None
4  P_002_2  C  None
         a  b     c
0    P_001  P  NYSE
1  P_001_1  C  NYSE
2    P_002  P  CHIX
3  P_002_1  C  CHIX
4  P_002_2  C  CHIX

If you would like to do that for the while dateset. 如果您想为while dateset执行此操作。

暂无
暂无

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

相关问题 Pandas Python 用另一个行值更新列 - Pandas Python update column with another row value 如何使用 Pandas 根据同一行中另一列的值替换一列中的 NaN 值? - How to replace NaN value in one column based on the value of another column in the same row using Pandas? Pandas 如果存在基于另一列值的行,则删除列值 - Pandas delete a column value if there exists a row based on another column value 如何根据行中的特定值和熊猫中的另一列对行进行分组? - How to group rows based on specific value in a row and another column in pandas? 如何根据 null 值将一个行列值分配给另一行列 - How to assign one row column value to another row column based on null value Pandas 根据 A 列中的行值更新 B 列中的行值 - Pandas update row value in column B based on row value in column A 根据另一列中的一列查找行值并进行计算 - Find row value based on one column in another column and do calculation 如何更新pandas数据框中另一列中特定值的行中的列值? - How to update a column value in a row for specific value in another column in pandas dataframe? Pandas DataFrame:为什么我不能通过行迭代基于另一列的值来更改一列的值? - Pandas DataFrame: Why I can't change the value of one column based on value of another through row iteration? pandas:根据另一列中的值计算每一行的jaccard相似度 - pandas:calculate jaccard similarity for every row based on the value in another column
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM