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.
通常需要對組進行運算。 但是有時候從每個組中獲得第一行或第n行是最高優先級。
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門票🎫?將于下周二開始銷售,此次銷售分為3節。 首先是早上10點,第二點是下午2點,最后一個是晚上8點。
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.
決定第一個用戶 🥇 每次會議的一次將免費參觀用戶在機票預訂期間指定的任何國家。
After the end of the day following user data was collected:
在一天結束之后,收集了以下用戶數據:
Comic-con user data Comiccon用戶數據 User Data 用戶數據從每個會話中提取第一個用戶 (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.
熊貓數據框具有groupby([column(s)])。first()方法,該方法用于從每個組獲取第一條記錄。
groupby on basis of session 根據會話分組 Penny didn’t put anything in the country field ? 竹enny在鄉村領域沒有放任何東西?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()的結果與最后一組稍有不同,即8PM ,其中Penny 是第一個拿到票的人。 由于某些原因, Penny將該country字段留空了,但以上結果包含了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😕
看來GroupBy.first方法有問題😕
The expected result should be
預期結果應該是
groupby.first() was expected to give the above result ?? groupby.first()預期會得到以上結果??There is nothing wrong with the groupby.first()method. It rather works that way.
groupby.first()方法沒有任何問題。 而是這樣工作的。
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.
因此,如果在第一個記錄中有一個空值,則該組中的第一個非空值將被帶入第一個記錄中。 這就是潘妮去澳大利亞旅游的原因。
What if the entire group contains the null values, then what should be expected from GroupBy.first?
如果整個組都包含空值,那么應該從GroupBy.first中得到什么呢?
Lets take a look at the last year comic-con where no one wanted to on a tour.
讓我們看一下去年的漫畫展,沒人想去參觀。
Last year comic-con 去年的動漫展 Last year comic-con user data 去年動漫用戶數據Getting the first user of each session.
獲取每個會話的第一個用戶 。
first user extraction 首次用戶提取 This time the result seems correct ?? 這次結果似乎正確??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.
結果與上述情況相同-即該組的記錄中將為空。
Is there a way to make it work to get the correct result using GroupBy.first?
有沒有一種方法可以使它使用GroupBy.first獲得正確的結果?
The way to fix this problem is to replace the np.nan(NaN) with None using the np.where()method.
解決此問題的方法是使用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. 塔達! 😁預期結果。 將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??.
有一種簡單的方法可以完成此任務,而無需擔心np.nan和None。 在這種情況下, GroupBy.nth會派上用場?? 。
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不會進行任何更改,即使組的第一條記錄中包含空值,它也會按順序給出結果。 此外,它還具有其他功能。
GroupBy.nth to get the first record GroupBy.nth獲得第一條記錄 Gives the expected result 給出預期的結果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 不與無替代和 GroupBy.nth 給出了預期的結果。 即使將 np.nan 替換為 None ,結果也保持不變
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.
現在,Comic-con希望從每個會話中選擇2個用戶。 First和Third用戶將是幸運的獲勝者。
GroupBy.nth can be used to get multiple specific records within each group.
GroupBy.nth可用于獲取每個組中的多個特定記錄。
Providing a list. Here, 0 for first record while 2 for third record. 提供清單。 此處,0為第一條記錄,而2為第三條記錄。 First and third record from each session 每個會話的第一和第三條記錄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.
最終,謝爾頓,倫納德和拉吉(霍華德😞除外)幸運的日子到了。 國家是由小組預先計劃的。 Comic-con決定從每個會話中獲取前3個條目。
Is there a way to get the first n records from each group?
有沒有辦法從每個組中獲取前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.
在這段時間里, GroupBy.head得以營救🏃?🏃?。 可以使用GroupBy.nth但是這里的GroupBy.nth是提供一個從0一直到n-1的列表序列。
GroupBy.head for first n records 前n條記錄的GroupBy.head First 3 records from each session 每個會話的前3條記錄The GroupBy.head preserves the index of the original dataframe.
GroupBy.head保留原始數據幀的索引。
GroupBy.head can also be used to get the first record from each group irrespective of np.nan(NaN) and None.
GroupBy.head也可以用于從每個組獲取第一條記錄,而不管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
總結
以上是生活随笔為你收集整理的groupby.nth_熊猫groupby first vs groupby nth vs groupby head的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 对计算机课的期待200字,谈《计算机应用
- 下一篇: echarts4.8.0最新版本下载,亲