-2

Pandas:I have a dataframe given below which contains the same set of banks twice..I need to slice the data from 0th index that contains a bank name, upto the index that contains the same bank name..here in the problem -DEUTSCH BANK AG..I need to apply same logic to any such kind of dataframes.ty..

I tried with logic:- df25.iloc[0,1]==df25[1].any().. but it returns nly true but not the index position.

DataFrame:-[1]:https://i.stack.imgur.com/iJ1hJ.png, https://i.stack.imgur.com/J2aDX.png

Andrej Kesely
  • 168,389
  • 15
  • 48
  • 91
  • Pls don't post images. https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples – abhilb Dec 20 '19 at 11:13

1 Answers1

1

You need to get the index of all the rows that has the value you are looking for (in this case the bank name) and get the slice the data frame using the indices.

Example:

df = pd.DataFrame({'Col1':list('abcdeafgbfhi')})
search_str = 'b'
idx_list = list(df[(df['Col1']==search_str)].index.values)
print(df[idx_list[0]:idx_list[1]])

Output:

  Col1
1    b
2    c
3    d
4    e
5    a
6    f
7    g

Note that the assumption is that there will be only 2 rows with the same value. If there are more than 2, you have to play with the index list values and get what you need. Hope this helps.

Keep in mind that posting a sample data set will always help you get more answers as people will move away to another question when they see images or screenshots, because it involves additional steps to reproduce the issue

davidbilla
  • 2,120
  • 1
  • 15
  • 26