groupby.nth_熊猫groupby first vs groupby nth vs groupby head
groupby.nth
Often there comes a need to compute operations on groups. But there are times when getting the first or the nth row from each group is the highest priority.
通常需要對(duì)組進(jìn)行運(yùn)算。 但是有時(shí)候從每個(gè)組中獲得第一行或第n行是最高優(yōu)先級(jí)。
Photo by Waldemar Brandt on Unsplash Waldemar Brandt在 Unsplash上 拍攝的照片Comic-con tickets🎫? were going on sale on next Tuesday and the sale was divided into 3 sessions. First in the morning at 10 AM, second at 2 PM and the last one at 8 PM.
Comic-con門(mén)票🎫?將于下周二開(kāi)始銷(xiāo)售,此次銷(xiāo)售分為3節(jié)。 首先是早上10點(diǎn),第二點(diǎn)是下午2點(diǎn),最后一個(gè)是晚上8點(diǎn)。
It was decided that the first user🥇 of each session will be given a free tour to any country that the user specifies during the ticket reservation.
決定第一個(gè)用戶 🥇 每次會(huì)議的一次將免費(fèi)參觀用戶在機(jī)票預(yù)訂期間指定的任何國(guó)家。
After the end of the day following user data was collected:
在一天結(jié)束之后,收集了以下用戶數(shù)據(jù):
Comic-con user data Comiccon用戶數(shù)據(jù) User Data 用戶數(shù)據(jù)從每個(gè)會(huì)話中提取第一個(gè)用戶 (Extracting the first user from each session)
Photo by Michal Balog on Unsplash Michal Balog在 Unsplash上 拍攝的照片Pandas dataframe has groupby([column(s)]).first() method which is used to get the first record from each group.
熊貓數(shù)據(jù)框具有groupby([column(s)])。first()方法,該方法用于從每個(gè)組獲取第一條記錄。
groupby on basis of session 根據(jù)會(huì)話分組 Penny didn’t put anything in the country field ? 竹enny在鄉(xiāng)村領(lǐng)域沒(méi)有放任何東西?The result of grouby.first() is going off the road a little bit with the last group — that is, 8PM where Penny was the first one to get the ticket. Due to some reasons Penny left the country field empty but the above result has Australia in it.
grouby.first()的結(jié)果與最后一組稍有不同,即8PM ,其中Penny 是第一個(gè)拿到票的人。 由于某些原因, Penny將該country字段留空了,但以上結(jié)果包含了Australia 。
As a result of which Penny was forced to go to Australia??. Poor Penny🤣.
因此,Penny被迫去澳大利亞??。 可憐的竹🤣。
It seems that there is something wrong with the GroupBy.first method😕
看來(lái)GroupBy.first方法有問(wèn)題😕
The expected result should be
預(yù)期結(jié)果應(yīng)該是
groupby.first() was expected to give the above result ?? groupby.first()預(yù)期會(huì)得到以上結(jié)果??There is nothing wrong with the groupby.first()method. It rather works that way.
groupby.first()方法沒(méi)有任何問(wèn)題。 而是這樣工作的。
So, if there is a null value in the first record then the first non-null value in the group is carried up into the first record. This is reason why Penny went on a tour to Australia.
因此,如果在第一個(gè)記錄中有一個(gè)空值,則該組中的第一個(gè)非空值將被帶入第一個(gè)記錄中。 這就是潘妮去澳大利亞旅游的原因。
What if the entire group contains the null values, then what should be expected from GroupBy.first?
如果整個(gè)組都包含空值,那么應(yīng)該從GroupBy.first中得到什么呢?
Lets take a look at the last year comic-con where no one wanted to on a tour.
讓我們看一下去年的漫畫(huà)展,沒(méi)人想去參觀。
Last year comic-con 去年的動(dòng)漫展 Last year comic-con user data 去年動(dòng)漫用戶數(shù)據(jù)Getting the first user of each session.
獲取每個(gè)會(huì)話的第一個(gè)用戶 。
first user extraction 首次用戶提取 This time the result seems correct ?? 這次結(jié)果似乎正確??What if there is only one record in group and the same record has null in it?
如果組中只有一條記錄并且同一條記錄中包含空值,該怎么辦?
The result stays the same as the above scenario — that is, the record of that group will have null in it.
結(jié)果與上述情況相同-即該組的記錄中將為空。
Is there a way to make it work to get the correct result using GroupBy.first?
有沒(méi)有一種方法可以使它使用GroupBy.first獲得正確的結(jié)果?
The way to fix this problem is to replace the np.nan(NaN) with None using the np.where()method.
解決此問(wèn)題的方法是使用np.where()方法將np.nan(NaN)替換為None 。
replacing np.nan(NaN) with None 用 None替換 np.nan(NaN) Ta-da!!! 😁 The expected result. After replacing np.nan(NaN) with None. 塔達(dá)! 😁預(yù)期結(jié)果。 將np.nan(NaN)替換為None后。There’s a simple way to get this things done without worrying about the np.nan and None. GroupBy.nth comes in handy during such situation??.
有一種簡(jiǎn)單的方法可以完成此任務(wù),而無(wú)需擔(dān)心np.nan和None。 在這種情況下, GroupBy.nth會(huì)派上用場(chǎng)?? 。
GroupBy.nth doesn’t change anything and gives the result as per order even though if the first record of the group has null value in it. Also, it has extra capabilities.
GroupBy.nth不會(huì)進(jìn)行任何更改,即使組的第一條記錄中包含空值,它也會(huì)按順序給出結(jié)果。 此外,它還具有其他功能。
GroupBy.nth to get the first record GroupBy.nth獲得第一條記錄 Gives the expected result 給出預(yù)期的結(jié)果The np.nan is not replaced with None and the GroupBy.nth gives the expected result. The result stays the same even if the np.nan are replaced with None
該 np.nan 不與無(wú)替代和 GroupBy.nth 給出了預(yù)期的結(jié)果。 即使將 np.nan 替換為 None ,結(jié)果也保持不變
GroupBy.nth has some extra powers
GroupBy.nth具有一些額外的功能
Now, Comic-con wants to select 2 users from each session. First and Third user will be the lucky winners.
現(xiàn)在,Comic-con希望從每個(gè)會(huì)話中選擇2個(gè)用戶。 First和Third用戶將是幸運(yùn)的獲勝者。
GroupBy.nth can be used to get multiple specific records within each group.
GroupBy.nth可用于獲取每個(gè)組中的多個(gè)特定記錄。
Providing a list. Here, 0 for first record while 2 for third record. 提供清單。 此處,0為第一條記錄,而2為第三條記錄。 First and third record from each session 每個(gè)會(huì)話的第一和第三條記錄Finally, the day has arrived when Sheldon, Leonard, and Raj (except Howard😞) got lucky. Country was pre-planned by the group. Comic-con decided to get first 3 entries from each session.
最終,謝爾頓,倫納德和拉吉(霍華德😞除外)幸運(yùn)的日子到了。 國(guó)家是由小組預(yù)先計(jì)劃的。 Comic-con決定從每個(gè)會(huì)話中獲取前3個(gè)條目。
Is there a way to get the first n records from each group?
有沒(méi)有辦法從每個(gè)組中獲取前n條記錄?
During such a time GroupBy.head comes to the rescue🏃?♂?. GroupBy.nth can be used but the catch here is to provide a sequence of list starting from 0 to all the way to n-1.
在這段時(shí)間里, GroupBy.head得以營(yíng)救🏃?🏃?。 可以使用GroupBy.nth但是這里的GroupBy.nth是提供一個(gè)從0一直到n-1的列表序列。
GroupBy.head for first n records 前n條記錄的GroupBy.head First 3 records from each session 每個(gè)會(huì)話的前3條記錄The GroupBy.head preserves the index of the original dataframe.
GroupBy.head保留原始數(shù)據(jù)幀的索引。
GroupBy.head can also be used to get the first record from each group irrespective of np.nan(NaN) and None.
GroupBy.head也可以用于從每個(gè)組獲取第一條記錄,而不管np.nan(NaN)和None 。
Safe journey to the winners.
獲勝者的安全旅程。
翻譯自: https://medium.com/swlh/pandas-groupby-first-vs-groupby-nth-vs-groupby-head-7f63fea5b870
groupby.nth
總結(jié)
以上是生活随笔為你收集整理的groupby.nth_熊猫groupby first vs groupby nth vs groupby head的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 对计算机课的期待200字,谈《计算机应用
- 下一篇: echarts4.8.0最新版本下载,亲