Home > Enterprise >  Can I parse a dataframe by row number?
Can I parse a dataframe by row number?

Time:02-25

I'm not sure if this is possible but say I have this:

In [1]: df = pd.DataFrame(np.random.rand(5,2),index=range(0,10,2),columns=list('AB'))

In [2]: df
Out[2]: 
          A         B
0  1.068932 -0.794307
2 -0.470056  1.192211
4 -0.284561  0.756029
6  1.037563 -0.267820
8 -0.538478 -0.800654

In [5]: df.iloc[[2]]
Out[5]: 
          A         B
4 -0.284561  0.756029

I know how to select a specific row(2 in above example) but say I want to create a new dataframe from above using 2 and beyond:

4 -0.284561  0.756029
6  1.037563 -0.267820
8 -0.538478 -0.800654

How can I do this?

CodePudding user response:

Using your answer you could change the iloc to have a : wildcard

df2 = df.iloc[1:]

This would let you look at everything 2 in the index

CodePudding user response:

Is this what you are looking for?

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.rand(5,2),index=range(0,10,2),columns=list('AB'))
df.iloc[2:]

enter image description here

  • Related