amy
amy

Reputation: 352

Get number of records before specific column value

I am trying to get few records before a certain value in column. I saw this link Get number of rows before and after a certain index value in pandas but this has the solution based on index. I want to implement it on column value.

import pandas as pd
d = {'col1': ['abc','bcd','string1','string2','jkl','opq']}
dfx=pd.DataFrame(d)

v1="string1"
history=pd.DataFrame()
history=history.append(dfx.loc[dfx['col1']==v1],ignore_index=True)
history

This code gives me only the matched record.

col1
string1

I want:

col1
abc
bcd
string1

I want records before that as well. I am sorry I just started to learn about slicing. I am stuck.

Upvotes: 0

Views: 84

Answers (2)

Sid R
Sid R

Reputation: 33

This solution assumes that your index is continuous and ascending. You could do .reset_index() if it isn't continuous. It also finds the first occurance of v1 in col1. Hope this helps:

import pandas as pd
d = {'col1': ['abc','bcd','string1','string2','jkl','opq']}
dfx=pd.DataFrame(d)

v1="string1"
history=pd.DataFrame()

# Get the FIRST index where col1 is equal to v1
v1_idx = dfx[dfx.col1 == v1].index[0]

# Make a list of indices to select 
select_idx = np.arange(0,v1_idx+1)

# Take subset and reset index
history=dfx.loc[dfx.index.isin(select_idx)].reset_index(drop=True)
history

Upvotes: 0

harpan
harpan

Reputation: 8631

You need to use .idxmax() to get the first occurance of string1 and then slice to that position.

history = dfx.iloc[:(dfx['col1']==v1).idxmax()+1]

Output:

    col1
0   abc
1   bcd
2   string1

Upvotes: 1

Related Questions