0

Imagine I have a dataframe as below. And I can print the output as

2
4
6

import pandas as pd

data = [[1,2,'a'],[3,4,'b'],[5,6,'c']]
columns = ['a','b','c']

df = pd.DataFrame(data ,columns = ['a','b','c'])

for i in df.itertuples():
    print(i.b)

Now Imagine I need to print the next line value from starting row always.How can I do that. Basically I need the output of only

4
6

How can I do it.

My approach is like below:

for i in df.itertuples():
   # here there should be a if function asking not to go for the last row (first row from upside down of the dataframe) 
     print((i+1).b)
suresh_chinthy
  • 377
  • 2
  • 12
  • 1
    Have you checked https://pandas.pydata.org/pandas-docs/stable/user_guide/indexing.html ? Just index the dataframe `df.loc[1:,'b']` – yatu Oct 08 '20 at 07:57
  • That approach does not work my requirement,,Basically I need to go for every row and from that next line should be read. – suresh_chinthy Oct 08 '20 at 08:01
  • 2
    Before doing that, you might want to read https://stackoverflow.com/a/55557758/9698684 . Iterating over a dataframe's rows, isn't a great idea, unless necessary. That's why indexing methods exist – yatu Oct 08 '20 at 08:01
  • Okay I might need to change whole program I just wrote,,will check how it goes well,,Thanks Mate – suresh_chinthy Oct 08 '20 at 08:13

0 Answers0