Suresh Raja Suresh Raja -4 years ago 141
Python Question

Pandas df.iterrows() method to access a set number of rows:

I am looping through a dataframe using df.iterrows(). Instead of looping through all the rows, I would like to set the number of rows accessed each time. First I would like to access the first two rows, then it will be the third to the sixth row and then the remaining. Is there a way to loop through the rows?

Here is what I have:

import pandas as pd
import numpy as np
df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))

df['key1'] = 0
df.key1.iloc[0:3] = 1
df.key1.iloc[3:7] = 2
df.key1.iloc[7:] = 3

df_grouped = df.groupby('key1')

for group_name, group_value in df_grouped:
fig, axes = plt.subplots(rows, 1, sharex=True, sharey=True, figsize= (15, 20))
for i,r in group_value.iterrows():
rows, columns = group_value.shape
r = r[0:columns-1]
r.plot(kind='bar', fill=False, log=False)

Answer Source

You could create a column according your condition, then do a group by over that column and iterated over the grouped data.

df = pd.DataFrame(np.random.randn(10, 4), columns=list('ABCD'))

df['key1'] = 0
df.key1.iloc[1:3] = 1
df.key1.iloc[3:7] = 2
df.key1.iloc[7:] = 3

df_grouped = df.groupby('key1')

for group_name, group_value in df_grouped:
    for i,r in group_value.iterrows():
        print i, max(r[:-1])
    print '-' * 80
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download