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
#1
I need your help to insert vales into my table based on a criteria.
My dataframe is as follows
df = pd.DataFrame([['2016-04-19',533247,1975000,np.nan], ['2016-04-19',533247,np.nan,1975000],['2016-05-05',np.nan,1947293.42,np.nan],['2016-05-05',np.nan,np.nan,1947293.42],['2016-06-03',535199,1930000,np.nan],['2016-06-04',535199,np.nan,1930000],['2016-08-04',np.nan,195000,np.nan],['2016-08-05',628490,5000,np.nan],['2016-08-05',628490,np.nan,5000],['2016-12-30',750200,15000,np.nan],['2016-12-31',326500,np.nan,15000]], columns=['DATE','CHQNO','Deposit','Withdrawal'])
I need to insert a value as 'RTN' into a new column call 'Status' if there are two records with same value in df[CHQNO] column & same value in df[Deposit] & df[Withdrawal] columns.

I'm expecting a dataframe like this


Output:
DATE        CHQNO Deposit        Withdrawal Status 0 2016-04-19 533247 1975000.0                 RTN 1 2016-04-19 533247                1975000.0 RTN 2 2016-05-05       1947293.42 3 2016-05-05                        1947293.42 4 2016-06-03 535199 1930000.0                 RTN 5 2016-06-04 535199                 1930000.0 RTN 6 2016-08-04         195000.0 7 2016-08-05 628490    5000.0                 RTN 8 2016-08-05 628490                     5000.0 RTN 9 2016-12-30 750200 15000.0 10 2016-12-31 326500                    15000.0
Thank you very much.
Moderator Larz60+: Please use output tags (I added this time)

Attached Files

Thumbnail(s)
   
Reply


Messages In This Thread
Insert values into a column in the same table based on a criteria - by klllmmm - Apr-13-2017, 05:54 AM

Possibly Related Threads…
Thread Author Replies Views Last Post
  attempt to split values from within a dataframe column mbrown009 9 2,755 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 970 Dec-12-2022, 03:22 AM
Last Post: ill8
  Increase df column values decimals SriRajesh 2 1,208 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 1,674 Jan-17-2022, 05:46 AM
Last Post: BrandonKastning
  New Dataframe Column Based on Several Conditions nb1214 1 1,927 Nov-16-2021, 10:52 PM
Last Post: jefsummers
  pandas: Compute the % of the unique values in a column JaneTan 1 1,885 Oct-25-2021, 07:55 PM
Last Post: jefsummers
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,725 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  Pandas Data frame column condition check based on length of the value aditi06 1 2,834 Jul-28-2021, 11:08 AM
Last Post: jefsummers
Question [Solved] How to refer to dataframe column name based on a list lorensa74 1 2,430 May-17-2021, 07:02 AM
Last Post: lorensa74
  Add column based on others timste 8 4,317 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