Pandas 数据框获取每组的第一行
- 2025-01-06 08:32:00
- admin 原创
- 122
问题描述:
我有一只DataFrame
如下的熊猫:
df = pd.DataFrame({'id' : [1,1,1,2,2,3,3,3,3,4,4,5,6,6,6,7,7],
'value' : ["first","second","second","first",
"second","first","third","fourth",
"fifth","second","fifth","first",
"first","second","third","fourth","fifth"]})
我想对此进行分组["id","value"]
并获取每组的第一行:
id value
0 1 first
1 1 second
2 1 second
3 2 first
4 2 second
5 3 first
6 3 third
7 3 fourth
8 3 fifth
9 4 second
10 4 fifth
11 5 first
12 6 first
13 6 second
14 6 third
15 7 fourth
16 7 fifth
预期成果:
id value
1 first
2 first
3 first
4 second
5 first
6 first
7 fourth
我尝试按照以下方法操作,但仅给出了第一行DataFrame
。
In [25]: for index, row in df.iterrows():
....: df2 = pd.DataFrame(df.groupby(['id','value']).reset_index().ix[0])
解决方案 1:
用于.first()
获取第一个(非空)元素。
>>> df.groupby('id').first()
value
id
1 first
2 first
3 first
4 second
5 first
6 first
7 fourth
如果需要id
如下列:
>>> df.groupby('id').first().reset_index()
id value
0 1 first
1 2 first
2 3 first
3 4 second
4 5 first
5 6 first
6 7 fourth
要获取前 n 条记录,您可以使用.head()
:
>>> df.groupby('id').head(2).reset_index(drop=True)
id value
0 1 first
1 1 second
2 2 first
3 2 second
4 3 first
5 3 third
6 4 second
7 4 fifth
8 5 first
9 6 first
10 6 second
11 7 fourth
12 7 fifth
解决方案 2:
如果您需要获取第一行,我建议使用.nth(0)
而不是。.first()
它们之间的区别在于它们处理 NaN 的方式,因此.nth(0)
无论该行中的值是什么都会返回组的第一行,而最终将返回每列中的.first()
第一个非值。 NaN
例如如果你的数据集是:
df = pd.DataFrame({'id' : [1,1,1,2,2,3,3,3,3,4,4],
'value' : ["first","second","third", np.NaN,
"second","first","second","third",
"fourth","first","second"]})
>>> df.groupby('id').nth(0)
value
id
1 first
2 NaN
3 first
4 first
和
>>> df.groupby('id').first()
value
id
1 first
2 second
3 first
4 first
解决方案 3:
这将为您提供每个组的第二行(零索引,nth(0)
与以下内容相同first()
):
df.groupby('id').nth(1)
文档:http://pandas.pydata.org/pandas-docs/stable/groupby.html#taking-the-nth-row-of-each-group
解决方案 4:
如果只需要每个组的第一行,我们就可以这样做drop_duplicates
,请注意函数默认方法keep='first'
。
df.drop_duplicates('id')
Out[1027]:
id value
0 1 first
3 2 first
5 3 first
9 4 second
11 5 first
12 6 first
15 7 fourth
解决方案 5:
也许这就是你想要的
import pandas as pd
idx = pd.MultiIndex.from_product([['state1','state2'], ['county1','county2','county3','county4']])
df = pd.DataFrame({'pop': [12,15,65,42,78,67,55,31]}, index=idx)
pop state1 county1 12 county2 15 county3 65 county4 42 state2 county1 78 county2 67 county3 55 county4 31
df.groupby(level=0, group_keys=False).apply(lambda x: x.sort_values('pop', ascending=False)).groupby(level=0).head(3)
> Out[29]:
pop
state1 county3 65
county4 42
county2 15
state2 county1 78
county2 67
county3 55
解决方案 6:
我认为“首先”意味着您已经根据需要对 DataFrame 进行了排序。
我所做的是:
df.groupby('id').agg('first') 我猜想“first”意味着你已经按需要对 DataFrame 进行了排序。我的做法是:
df.groupby('id').agg('first')
value
id
1 first
2 first
3 first
4 second
5 first
6 first
7 fourth
好处在于你可以插入任何你想要的功能:
df.groupby('id').agg(['first','last','count']))
value
first last count
id
1 first second 3
2 first second 2
3 first fifth 4
4 second fifth 2
5 first first 1
6 first third 3
7 fourth fifth 2
输出 DataFrame 具有 MultiIndex 列
MultiIndex([('value', 'first'),
('value', 'last'),
('value', 'count')],
)
解决方案 7:
take
您可以使用接受元素索引列表的方法进行选择:
df.groupby('id').take([0])
解决方案 8:
考虑到该'id'
列是数字类型,例如int32
/ int64
,也可以使用groupby.rank()
如下方法
[In]: df[df.groupby('value')['id'].rank() == 1]
[Out]:
id value
0 1 first
6 3 third
7 3 fourth
8 3 fifth
如果要重置索引,只需传递.reset_index()
如下内容
[In]: df[df.groupby('value')['id'].rank() == 1].reset_index()
[Out]:
index id value
0 0 1 first
1 6 3 third
2 7 3 fourth
3 8 3 fifth
如果不需要index
和列id
[In]: df.drop(['index', 'id'], axis=1, inplace=True)
[Out]:
value
0 first
1 third
2 fourth
3 fifth
扫码咨询,免费领取项目管理大礼包!