How to filter a pandas dataframe from start till it finds a matching string
Question:
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
- 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
- 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!
Answers:
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
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
- 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
- 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!
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