user1165419
user1165419

Reputation: 663

pandas iterate rows and then break until condition

I have a column that's unorganized like this;

Name
Jack
James
Riddick

Random value
Another random value

What I'm trying to do is get only the names from this column, but struggling to find a way to differentiate real names to random values. Fortunately the names are all together, and the random values are all together as well. The only thing I can do is iterate the rows until it gets to 'Random value' and then break off.

I've tried using lambda's for this but with no success as I don't think there's a way to break. And I'm not sure how comprehension could work in this case.

Here's the example I've been trying to play with;

df['Name'] = df['Name'].map(lambda x: True if x != 'Random value' else break)

But the above doesn't work. Any suggestions on what could work based on what I'm trying to achieve? Thanks.

Upvotes: 1

Views: 1360

Answers (1)

A.Kot
A.Kot

Reputation: 7903

Find index of row containing 'Random value':

index_split = df[df.Name == 'Random value'].index.values[0]

Save your random values column for use later if you want:

random_values = df.iloc[index_split+1:,].values[0]

Remove random values from the Names column:

df = df[0:index_split]

Upvotes: 1

Related Questions