[英]Pandas df.iterrows() method to access a set number of rows:
I am looping through a dataframe using df.iterrows(). 我正在使用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)
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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.