Home > OS >  How to remove index from pandas DataFrame.str.contains return
How to remove index from pandas DataFrame.str.contains return

Time:07-07

I am trying to find a substring within a pandas dataframe. I can find the substring with the method str.contains(), but that returns the row number index where the substring was found. I do not want that row number index.

df = pd.DataFrame({'Name': [Jordan, Pippen, Rodman], 'Number': [23, 33, 91]})
data = df[df['Name'].str.contains('Pip', case=False, regex=False)]
print(data)

Actual Output:

     Name     Number
1    Pippen     33 

Expected Output:

Name     Number
Pippen     33

I have tried the following to no avail:

data.style.hide(axis='index')
data.style.hide_index()
data = data.reset_index(drop=True)

For further clarification, see enter image description here

CodePudding user response:

Basically you cannot have a dataframe without an index. Even for an empty dataframe the index is Index([]) just an empty index but its there.

Not sure why would you want to do this. But you can set the index to empty strings as indices if that's what you want.

data.set_index(pd.Index(['']*len(data)), inplace=True)

print(data):

    Name  Number
  Pippen      33
  • Related