[英]Out of every 7 rows, get the nth row pandas
I have a df
like this that's about 1000 rows: 我有一个像这样的
df
大约1000行:
0 1
0 1.345 2.456
1 2.123 3.564
2 0.023 3.548
3 3.457 2.456
4 1.754 3.564
5 0.905 3.548
6 3.674 7.543
7 9.443 6.4433...
The way it's organized is every 7 rows constitutes one "set" of data (data cannot be sorted here). 它的组织方式是每7行构成一组“数据”(数据不能在这里排序)。 Within each of the "groups" of 7 rows I want to get the first row so my new data frame would look like:
在7行的每个“组”中,我想获得第一行,所以我的新数据框看起来像:
0 1
0 1.345 2.456
7 9.443 6.4433
I can solve it by creating a new column that repeats 1-7 & filtering by only that column... 我可以通过创建一个重复1-7并仅按该列过滤的新列来解决它...
0 1 groupby_col
0 1.345 2.456 1
1 2.123 3.564 2
2 0.023 3.548 3
3 3.457 2.456 4
4 1.754 3.564 5
5 0.905 3.548 6
6 3.674 7.543 7
7 9.443 6.4433 1...
then... 然后...
df[df['groupby_col'] == 1]
Is there a way I can do this in pandas without having to create an additional column then filter? 我有没有办法在熊猫中做到这一点,而无需创建额外的列然后过滤?
Option 1: 选项1:
In [54]: df.iloc[::7]
Out[54]:
0 1
0 1.345 2.4560
7 9.443 6.4433
Option 2: 选项2:
In [53]: df.iloc[np.arange(len(df))%7==0]
Out[53]:
0 1
0 1.345 2.4560
7 9.443 6.4433
df.loc[df.index%7==0]
Out[124]:
0 1
0 1.345 2.4560
7 9.443 6.4433
Or 要么
df.groupby(df.index//7,as_index=False).first()
Out[128]:
0 1
0 1.345 2.4560
1 9.443 6.4433
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.