久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

Pandas - 有條件的刪除重復項

Pandas - Conditional drop duplicates(Pandas - 有條件的刪除重復項)
本文介紹了Pandas - 有條件的刪除重復項的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

我有一個適用于 Python 3.6x 的 Pandas 0.19.2 數據框,如下所示.我想基于條件邏輯使用相同的 Id drop_duplicates().

I have a Pandas 0.19.2 dataframe for Python 3.6x as below. I want to drop_duplicates() with the same Id based on a conditional logic.

import pandas as pd
import numpy as np
np.random.seed(1)
df = pd.DataFrame({'Id':[1,2,3,4,3,2,6,7,1,8],
              'Name':['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'K'],
              'Size':np.random.rand(10),
              'Age':[19, 25, 22, 31, 43, 23, 44, 20, 51, 31]})

根據我在下面描述的邏輯,實現這一目標的最有效(如果可能的話)方法是什么?

What would be the most efficient (if possible vectorised) way to achieve this based on the logic I describe below?

1) 在刪除重復項之前,將重復的 Id 條目的 Size 相加.

1) Before dropping duplicates, sum the Size of duplicate Id entries.

2) 刪除相同 Id 記錄的重復記錄,保留具有較大 Age 記錄的記錄.

2) Drop duplicates for same Id records, keeping the one that has a larger Age.

期望的輸出是:

   Age  Id Name      Size
1   25   2    B  0.812662
3   31   4    D  0.302333
4   43   3    E  0.146870
6   44   6    G  0.186260
7   20   7    H  0.345561
8   51   1    I  0.813790
9   31   8    K  0.538817

推薦答案

使用GroupBy.transform 用于與 sort_valuesdrop_duplicates 用于刪除重復:

Use GroupBy.transform for aggregated values with same size as original DataFrame with sort_values and drop_duplicates for remove dupes:

df['Size'] = df.groupby('Id')['Size'].transform('sum')
df = df.sort_values('Age').drop_duplicates('Id', keep='last').sort_index()
print (df)
   Id Name      Size  Age
1   2    B  0.812663   25
3   4    D  0.302333   31
4   3    E  0.146870   43
6   6    G  0.186260   44
7   7    H  0.345561   20
8   1    I  0.813789   51
9   8    K  0.538817   31

這篇關于Pandas - 有條件的刪除重復項的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!

相關文檔推薦

Python Pandas: Counting the frequency of a specific value in each row of dataframe?(Python Pandas:計算每行數據幀中特定值的頻率?)
Pandas, groupby and summing over specific months(Pandas、groupby 和特定月份的求和)
.sum() method in pandas gives inconsistent results(pandas 中的 .sum() 方法給出的結果不一致)
Sum product and groupby(對產品和分組求和)
How to sum in pandas by unique index in several columns?(如何通過幾列中的唯一索引對 pandas 求和?)
Pandas: sum up multiple columns into one column without last column( pandas :將多列匯總為一列,沒有最后一列)
主站蜘蛛池模板: 韩日av在线 | 亚洲精品久久久久久久久久久 | 亚洲在线| 97国产在线视频 | 中文在线一区 | 丁香九月婷婷 | 99热| 国产麻豆xxxvideo实拍 | 免费v片 | 成人免费视屏 | 午夜在线视频观看日韩17c | 在线观看日韩av | 欧美人与性动交α欧美精品 | 免费看黄色的视频 | 国产1区2区3区 | 国产成人三级 | 婷婷天堂| 一区二区三区国产精品 | 黄色一级大片 | 日皮视频在线观看 | 日本精品在线视频 | 少妇一级淫片免费看 | wwwxxx欧美 | 国产精品成人一区二区网站软件 | 美女无遮挡网站 | 国产永久精品 | 午夜精品久久久 | 毛片毛片毛片 | 国产成人午夜高潮毛片 | 一区二区精品 | 一级黄色片免费 | 成人黄色av | 狠狠的操 | 日本特级淫片 | 亚洲日本在线观看 | 免费视频a| 免费看的毛片 | 超碰在线小说 | 午夜成人影片 | 日韩欧美国产综合 | 亚洲免费视频网站 |