Rilcon42
Rilcon42

Reputation: 9763

groupby select value only if match

I got my data sorted correctly, but now Im trying to find a way to group by "first not empty string value". Is there a way to do this without changing the rest of the data? First was close, but not quite what I needed

grouped = sortedvals.groupby(['name']).first().reset_index()

doesnt work if the first value is empty ie: '',2 (my goal is to return 2) but does work for everything else.

Upvotes: 0

Views: 53

Answers (1)

BENY
BENY

Reputation: 323276

Use replace function to replace blank values with np.nan

import numpy as np

grouped = sortedvals.replace('',np.nan).groupby(['name']).first().reset_index()

Upvotes: 1

Related Questions