Python Forum
How to get previous non empty value of another column
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to get previous non empty value of another column
#1
I'm trying to get the previous non-empty value of another column.

Sample of my data frame
import pandas as pd
table = pd.DataFrame(data = {'CustName':['Customer A','','','Customer B','',''],
                             'Value':[1500,1400,9000,9000,9000,1600],
                             })
I'm expecting a table like this
Output:
CustName CustNameNew Value 0 Customer A Customer A 1500 1 Customer A 1400 2 Customer A 9000 3 Customer B Customer B 9000 4 Customer B 9000 5 Customer B 1600
Can someone help me to create "CustNameNew" column through a function using the previous non-empty value of "CustName"?
Reply
#2
Managed to do this

table['CustName'].replace('', np.nan,  inplace=True)

table['CustName'].fillna(method ='pad',  inplace=True)
Output:
CustName Value 0 Customer A 1500 1 Customer A 1400 2 Customer A 9000 3 Customer B 9000 4 Customer B 9000 5 Customer B 1600
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  [SOLVED] [sqilte3] Check if column not empty? Winfried 5 1,117 Jan-28-2023, 12:53 PM
Last Post: Winfried
  write to excel will be empty in column jacklee26 7 3,358 Jun-27-2020, 12:09 AM
Last Post: snippsat
  find empty cells in a column Pedroski55 2 23,813 Sep-18-2017, 01:27 PM
Last Post: Pedroski55
  Open previous csv file to read value under column for comparison DBS 3 3,902 Jan-13-2017, 07:09 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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