Issue
I have a function that searches for a term within a DataFrame and I would like to return an integer based index of the found term, I checked the docs and think that Index.get_loc() should do the trick however I am getting the following error with my code:
df = pd.read_excel(os.path.join(MEDIA_ROOT, "files", file), sheet_name=sheet)
for col in df.columns:
rowfilter = df[col].map(lambda i: str(i)).str.contains("quantity", case=False, regex=False)
search_row = df[rowfilter].index.get_loc() #This is a 'slice' of the rows containing the search term
print(search_row)
#The output should be 6
However, I am getting the following error,
Index.get_loc() missing 1 required positional argument: 'key'
I have tried the following:
search_row = df[rowfilter].index()
print(pd.Index.get_loc(search_row))
But get the same error, So the question is, what is the correct key?
Solution
As an aside, you can simplify your search:
mask = df[col].astype(str).str.contains('quantity', case=False, regex=False)
In any case, once you obtain your mask
(which is a bool
Series
), you can get the numerical indices of all the matches:
ix = df.reset_index().index[mask]
This will be an Int64Index
. You can also get it as a list
:
ixs = ix.tolist()
In any case, you can use ix
or ixs
for .iloc
(df.iloc[ix]
).
Answered By - Pierre D
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.