Home > Net >  How to filter a pandas dataframe from start till it finds a matching string
How to filter a pandas dataframe from start till it finds a matching string

Time:07-20

I have a data frame like this:

df:
name    score
Coby0    8
Sony1    3
Coby1    4
Sony2    6
Coby2    7
Sony3    8
Coby3    3
Sony4    2
Coby4    9
Sony5    5
Coby5    7
Sony6    2
Coby6    10
  1. I want to filter this data frame from the start till it finds the first row that starts with 'Sony'
name    score
Coby0    8
Sony1    3
  1. I want to filter this data from the start till it finds the last row that starts with 'Sony'
df:
name    score
Coby0    8
Sony1    3
Coby1    4
Sony2    6
Coby2    7
Sony3    8
Coby3    3
Sony4    2
Coby4    9
Sony5    5
Coby5    7
Sony6    2

Thanks in advance!

CodePudding user response:

You can use

m = df['name'].str.startswith('Sony')

first_true_idx = m.idxmax()
df1 = df.iloc[:first_true_idx 1]
print(df1)

    name  score
0  Coby0      8
1  Sony1      3
last_true_idx = m[::-1].idxmax()
df2 = df.iloc[:last_true_idx 1]
print(df2)

     name  score
0   Coby0      8
1   Sony1      3
2   Coby1      4
3   Sony2      6
4   Coby2      7
5   Sony3      8
6   Coby3      3
7   Sony4      2
8   Coby4      9
9   Sony5      5
10  Coby5      7
11  Sony6      2

CodePudding user response:

Here is the simplest was that I know to find the first and last column.

import pandas as pd
foo = [['Coby0',8],['Sony0',3],['Coby1',4],['Sony1',6],['Coby2',7],['Sony2',8]]
df = pd.DataFrame(foo, columns=['name','score'])
print(df.head())

first = df[df.name.str.startswith('Sony')].iloc[0]
print(first)
last = df[df.name.str.startswith('Sony')].iloc[-1]
print(last)
  • Related