2

I have a data frame like this,

   Name    Product    Quantity
0  NaN     1010       10
1  NaN     2010       12
2  NaN     4145       18
3  NaN     5225       14
4  Total   6223       16
5  RRA     7222       18
6  MLQ     5648       45

Now, I need to extract rows/new dataframe that has rows until Total that is in Name column.

Output needed:

       Name    Product    Quantity
    0  NaN     1010       10
    1  NaN     2010       12
    2  NaN     4145       18
    3  NaN     5225       14

I tried this,

df[df.Name.str.contains("Total", na=False)]

This is not helpful for now. Any suggestion would be great.

user9431057
  • 1,203
  • 1
  • 14
  • 28

1 Answers1

6

Select the index where the True value is located and slice using df.iloc:

df_new=df.iloc[:df.loc[df.Name.str.contains('Total',na=False)].index[0]]

or using series.idxmax() which allows you to get the index of max value (max of True/False is True):

df_new=df.iloc[:df.Name.str.contains('Total',na=False).idxmax()]

print(df_new)

  Name  Product  Quantity
0  NaN     1010        10
1  NaN     2010        12
2  NaN     4145        18
3  NaN     5225        14
anky
  • 74,114
  • 11
  • 41
  • 70
  • a follow up question, if I have to use an `or` operation, let say I want to extract row until `Total` or `Subtotal` how can I do that? If I have to do for a list of files and that has different key words that I need to extract until, then it can be helpful. Any ideas? – user9431057 Jan 27 '19 at 17:50
  • 1
    @user9431057 check this it will help you: https://stackoverflow.com/questions/26577516/how-to-test-if-a-string-contains-one-of-the-substrings-in-a-list – anky Jan 27 '19 at 17:58