Python Forum

Full Version: How to get previous non empty value of another column
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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"?
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