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

Pandas - 根據條件復制行

Pandas - Duplicate Row based on condition(Pandas - 根據條件復制行)
本文介紹了Pandas - 根據條件復制行的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!

問題描述

如果行滿足條件,我正在嘗試創(chuàng)建重復行.在下表中,我根據 groupby 創(chuàng)建了一個累積計數,然后再計算 groupby 的 MAX.

I'm trying to create a duplicate row if the row meets a condition. In the table below, I created a cumulative count based on a groupby, then another calculation for the MAX of the groupby.

df['PathID'] = df.groupby(DateCompleted).cumcount() + 1
df['MaxPathID'] = df.groupby(DateCompleted)['PathID'].transform(max)

Date Completed    PathID    MaxPathID
1/31/17           1         3
1/31/17           2         3
1/31/17           3         3
2/1/17            1         1
2/2/17            1         2
2/2/17            2         2

在這種情況下,我只想復制 2/1/17 的記錄,因為該日期只有一個實例(即 MaxPathID == 1).

In this case, I want to duplicate only the record for 2/1/17 since there is only one instance for that date (i.e. where the MaxPathID == 1).

期望的輸出:

Date Completed    PathID    MaxPathID
1/31/17           1         3
1/31/17           2         3
1/31/17           3         3
2/1/17            1         1
2/1/17            1         1
2/2/17            1         2
2/2/17            2         2

提前致謝!

推薦答案

我認為你需要通過 Date Completed 獲取 unique 行,然后 concat 行到原始:

I think you need get unique rows by Date Completed and then concat rows to original:

df1 = df.loc[~df['Date Completed'].duplicated(keep=False), ['Date Completed']]
print (df1)
  Date Completed
3         2/1/17

df = pd.concat([df,df1], ignore_index=True).sort_values('Date Completed')
df['PathID'] = df.groupby('Date Completed').cumcount() + 1
df['MaxPathID'] = df.groupby('Date Completed')['PathID'].transform(max)
print (df)
  Date Completed  PathID  MaxPathID
0        1/31/17       1          3
1        1/31/17       2          3
2        1/31/17       3          3
3         2/1/17       1          2
6         2/1/17       2          2
4         2/2/17       1          2
5         2/2/17       2          2

print (df)
  Date Completed  a  b
0        1/31/17  4  5
1        1/31/17  3  5
2        1/31/17  6  3
3         2/1/17  7  9
4         2/2/17  2  0
5         2/2/17  6  7

df1 = df[~df['Date Completed'].duplicated(keep=False)]
#alternative - boolean indexing by numpy array
#df1 = df[~df['Date Completed'].duplicated(keep=False).values]
print (df1)
  Date Completed  a  b
3         2/1/17  7  9

df = pd.concat([df,df1], ignore_index=True).sort_values('Date Completed')
print (df)
  Date Completed  a  b
0        1/31/17  4  5
1        1/31/17  3  5
2        1/31/17  6  3
3         2/1/17  7  9
6         2/1/17  7  9
4         2/2/17  2  0
5         2/2/17  6  7

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

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

相關文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個矩形邊界框中應用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據文本方向檢測圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測圖像中矩形的中心和角度)
主站蜘蛛池模板: 自拍偷拍第一页 | 日韩有码一区 | 久久综合av | 天天拍天天操 | 日韩av在线不卡 | 影音先锋中文字幕在线观看 | 国产目拍亚洲精品99久久精品 | 欧美一区二区三区在线观看 | 久久一起草| 国产精品精品视频一区二区三区 | 羞羞视频在线观看免费观看 | 日韩精品一区二区久久 | 91av视频在线播放 | 亚洲精品一区二三区不卡 | 日韩一区二区在线免费观看 | 亚洲麻豆 | 一区二区小视频 | 成人欧美一区二区三区黑人孕妇 | 日韩av一区二区在线观看 | 91伊人| 日本电影一区二区 | 偷拍亚洲色图 | 一本色道久久综合亚洲精品高清 | 性欧美xxxx | 国产99久久精品 | 日韩在线免费视频 | 911网站大全在线观看 | 成人网在线看 | 日本 欧美 三级 高清 视频 | 欧美激情99 | 国产激情一区二区三区 | 久久久.com | 免费一区二区三区 | 日韩高清成人 | 日韩欧美三区 | 毛片网在线观看 | 午夜免费网站 | 国精品一区二区 | 两性午夜视频 | 噜久寡妇噜噜久久寡妇 | 人人做人人澡人人爽欧美 |