How to get last non empty value of each columns in pandas?

I have a dataframe of half rectangle, something like this

   a_1    a_2   a_3     a_4
1  Apple  Nuts  Plum    Cucumber
2 Grapes  Kiwi  Apple    ''
3  Melon  Lime   ''      ''
4  Peach  ''     ''      ''

I want a list of last non empty value of each columns. So the output expecting is -

['Peach', 'Lime', 'Apple', 'Cucumber']

Upvotes: 1

Views: 1906

Answers (2)

Clément Perroud
Clément Perroud

Reputation: 533

The easiest way in my opinion is to make a for loop that check every value for each column and store it when it is not empty.

You make it this way:

import pandas as pd
data = pd.DataFrame({
    "a_1": ["Apple", "Grapes", "Melon", "Peach"],
    "a_2": ["Nuts", "Kiwi", "Lime", ""],
    "a_3": ["Plum", "Apple", "", ""],
    "a_4": ["Cucumber", "", "", ""]
})

last_non_empty_values =[]
for column in data.columns:
    n = len(data[column])
    for i in range(n-1 ,-1, -1):
        if data[column][i] != "":
            last_non_empty_values.append(data[column][i])
            break


print(last_non_empty_values)

Or if your half-rectangle does not contain empty values. You can make it easier this way :

import pandas as pd
data = pd.DataFrame({
    "a_1": ["Apple", "Grapes", "Melon", "Peach"],
    "a_2": ["Nuts", "Kiwi", "Lime", ""],
    "a_3": ["Plum", "Apple", "", ""],
    "a_4": ["Cucumber", "", "", ""]
})

last_non_empty_values =[data[column][len(data[column])-1 - index] for index, column in enumerate(data.columns)]

print(last_non_empty_values)

Both examples correctly display the following output:

['Peach', 'Lime', 'Apple', 'Cucumber']

Upvotes: 1

jezrael
jezrael

Reputation: 862581

First create mising values instead empty strings, forward filling them and select last row by iloc:

L = df.replace('', np.nan).ffill().iloc[-1].tolist()
print (L)
['Peach', 'Lime', 'Apple', 'Cucumber']

Upvotes: 2

Related Questions