0

I'm trying to iterate through a dataframe, where I've indexed the data using a series of string names. I want to search these indices for a certain substring and then place all those rows that have the substring into another dataframe.

suppose my original dataframe is called df.

I've tried:

find_cat = df[df.str.contains("cat")]

And also tried

find_cat = 

for row_index,row in df.iterrows():
    if row_index.str.contains("cat", na=True):
        find_cat(append(row)

Both of these fail [try 1 = error in syntax, try 2 = an empty dataframe.

Quite new to python and Pandas....any help greatly appreciated!

Katie Melosto
  • 411
  • 2
  • 8
  • [This answer of mine](https://stackoverflow.com/a/55335207/4909087) shows how to select by partial substring search. – cs95 Sep 13 '20 at 19:27
  • Thank you @cs95. I'm having some difficulty because somehow it's not letting me search through a column because the data is indexed by name but "Name" is not showing as a column – Katie Melosto Sep 13 '20 at 19:31
  • Use `df = df.reset_index()` to convert the index into a column. Otherwise you can use `df.index.str.contains(...)` – cs95 Sep 13 '20 at 19:32

0 Answers0