Python Forum
Insert values into a column in the same table based on a criteria
Thread Rating:
  • 2 Vote(s) - 2.5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Insert values into a column in the same table based on a criteria
#2
You can check for lines that have same CHQNO as following line and deposit is same as following withdrawal (first_one) and for lines that has same CHQNO as previous line and withdrawal is same as previous deposit (second_one).

This works only for deposit "followed" by withdrawal, otherwise you would need to either sort it or use more conditions of same type. Or it could be done by "SQL-joining" dataframe with itself on chqno and withdrawal==deposit followed by some cleaning/processing.
Output:
In [4]: first_one = (df.CHQNO == df.CHQNO.shift(-1)) & (df.Deposit == df.Withdrawal.shift(-1)) In [5]: second_one = (df.CHQNO == df.CHQNO.shift(1)) & (df.Deposit.shift(1) == df.Withdrawal) In [6]: df['rtn'] = np.where(first_one | second_one, "RTN", "") In [7]: df Out[7]:           DATE     CHQNO     Deposit  Withdrawal  rtn 0   2016-04-19  533247.0  1975000.00         NaN  RTN 1   2016-04-19  533247.0         NaN  1975000.00  RTN 2   2016-05-05       NaN  1947293.42         NaN      3   2016-05-05       NaN         NaN  1947293.42      4   2016-06-03  535199.0  1930000.00         NaN  RTN 5   2016-06-04  535199.0         NaN  1930000.00  RTN 6   2016-08-04       NaN   195000.00         NaN      7   2016-08-05  628490.0     5000.00         NaN  RTN 8   2016-08-05  628490.0         NaN     5000.00  RTN 9   2016-12-30  750200.0    15000.00         NaN      10  2016-12-31  326500.0         NaN    15000.00    
Reply


Messages In This Thread
RE: Insert values into a column in the same table based on a criteria - by zivoni - Apr-13-2017, 08:29 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  attempt to split values from within a dataframe column mbrown009 9 6,432 Jun-20-2024, 07:59 PM
Last Post: AdamHensley
  Make unique id in vectorized way based on text data column with similarity scoring ill8 0 1,527 Dec-12-2022, 03:22 AM
Last Post: ill8
  Increase df column values decimals SriRajesh 2 2,042 Nov-14-2022, 05:20 PM
Last Post: deanhystad
Question How does one clean a populated table in MySQL/MariaDB? Copying values across tables? BrandonKastning 2 2,347 Jan-17-2022, 05:46 AM
Last Post: BrandonKastning
  New Dataframe Column Based on Several Conditions nb1214 1 2,648 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  pandas: Compute the % of the unique values in a column JaneTan 1 2,536 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  update values in one dataframe based on another dataframe - Pandas iliasb 2 13,994 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 3,960 Jul-28-2021, 11:08 AM
Last Post: jefsummers
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 3,142 May-17-2021, 07:02 AM
Last Post: lorensa74
  Add column based on others timste 8 5,910 Apr-03-2021, 07:39 AM
Last Post: devesh_sahu

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020