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

Pandas - 根據(jù)條件復(fù)制行

Pandas - Duplicate Row based on condition(Pandas - 根據(jù)條件復(fù)制行)
本文介紹了Pandas - 根據(jù)條件復(fù)制行的處理方法,對(duì)大家解決問(wèn)題具有一定的參考價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧!

問(wèn)題描述

如果行滿足條件,我正在嘗試創(chuàng)建重復(fù)行.在下表中,我根據(jù) groupby 創(chuàng)建了一個(gè)累積計(jì)數(shù),然后再計(jì)算 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

在這種情況下,我只想復(fù)制 2/1/17 的記錄,因?yàn)樵撊掌谥挥幸粋€(gè)實(shí)例(即 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

提前致謝!

推薦答案

我認(rèn)為你需要通過(guò) 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

這篇關(guān)于Pandas - 根據(jù)條件復(fù)制行的文章就介紹到這了,希望我們推薦的答案對(duì)大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

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

相關(guān)文檔推薦

How to draw a rectangle around a region of interest in python(如何在python中的感興趣區(qū)域周圍繪制一個(gè)矩形)
How can I detect and track people using OpenCV?(如何使用 OpenCV 檢測(cè)和跟蹤人員?)
How to apply threshold within multiple rectangular bounding boxes in an image?(如何在圖像的多個(gè)矩形邊界框中應(yīng)用閾值?)
How can I download a specific part of Coco Dataset?(如何下載 Coco Dataset 的特定部分?)
Detect image orientation angle based on text direction(根據(jù)文本方向檢測(cè)圖像方向角度)
Detect centre and angle of rectangles in an image using Opencv(使用 Opencv 檢測(cè)圖像中矩形的中心和角度)
主站蜘蛛池模板: 91久久精品国产91久久性色tv | 亚洲国产午夜 | www.99热这里只有精品 | 成人免费看 | 国产成人精品一区二区三区 | 青青久久久 | 色男人的天堂 | 综合久久av | 国产视频精品在线观看 | 狠狠操av | 国产精品污www一区二区三区 | 久久之精品 | 正在播放一区二区 | 午夜精品 | 美女久久 | 亚洲精品欧美 | 欧美精品在线观看 | 欧美 日韩 亚洲91麻豆精品 | 国产h视频 | 欧美成人精品一区二区三区 | 国产精品亚洲欧美日韩一区在线 | 99精品免费久久久久久久久日本 | h片在线观看免费 | 天天草天天干天天 | 丁香五月网久久综合 | 欧美一级欧美三级在线观看 | 亚洲在线免费观看 | 日日操av| 一区二区三区亚洲 | 欧美视频成人 | 成人在线视频免费观看 | 特黄色毛片 | 欧美成人一区二区 | 天天干com | 久久精品 | 中国一级特黄毛片大片 | 尤物在线精品视频 | 日韩在线精品视频 | 亚洲一区二区三区在线免费观看 | 国产精品网址 | 亚洲国产精品一区二区久久 |