pandas分組後,如何對分組後的資料進行遍歷處理?

時間 2022-01-10 22:56:36

1樓:

import

pandas

aspd

import

numpy

asnp

train_data=pd

.read_csv

("test_v1.txt"

)train_data

.groupby

("pro_id"

)["dt"].

agg([

"max"

,"min"

])def

unique_num(x

):arr=np

.unique(x

)return[x

forx

inarr]df

=train_data

.groupby

('pro_id'

)["dt"

,"brand_name"

,"catalog_name"].

agg(

unique_num)df

=df.reset_index

()import

datetime

defprocess_date

(row

):#print(row)

dts=

[str(x

)forxin

row[1]]

dts=

set(

dts)

end=

max(

dts)

start

=min

(dts

)try:dt

=datetime

.datetime

.strptime

(str

(start

),"%Y%m%d"

)except

:print

(row[0

])return

""arr=

fori

inrange

(1000):x

=(dt+

datetime

.timedelta

(days=i

)).strftime

("%Y%m%d"

)ifx==

str(

end)

:breakifx

notin

dts:

arr.(x

)return

",".

join

(arr)df

["not_in"]=

df.(process_date

,axis=1

)kv=forxin

train_data

.columns

}output=pd

.DataFrame(kv

)count=0

ids=

dts=

brands=

catlogs=

fortup

inzip(df

["pro_id"

],df

["not_in"

],df

["brand_name"

],df

["catalog_name"

]):#print(tup,type(tup[1:]))id=tup[0]

brand

=tup[2

][0]catlog

=tup[3

][0]foriin

tup[1]

.split

(","

):ifi==

"":break

ids.(id

)dts.(

i)brands.(

brand

)catlogs.(

catlog)df

[df.pro_id

=="not_in"

]output

["pro_id"]=

idsoutput

["dt"]=

dtsoutput

["brand_name"]=

brands

output

["catalog_name"]=

catlogs

output

.to_csv

("test_not.csv"

,header

=False

,index

=False)

如何根據原檔名對檔案進行分組,然後把分組依自然數順序排列,最後按分組編號重新命名?

我用Total Commander操作,不用程式設計就能簡單實現重新命名 不過我只是拿幾十個檔案來測試,像內容中 幾萬個 這個數量級,不知道效果如何。以下是過程 1 利用TC的快速搜尋,選中所有檔名帶有 1的檔案,CTRL M喚出重新命名工具,規則填寫JF C 4 1 2 回到檔案列表,選中所有包含...

R語言中對乙個資料框進行分組後(group by)如何將同一組聚在一起顯示(不按原來的順序)

library tidyverse set seed 12 df tibble a sample 100,5 b sample 100,5 id sample LETTERS 1 2 5,replace TRUE df Atibble 5 x3 a bid int 1 6669A 290100B 3...

《這就是街舞》第三季分組後,哪個隊長的組實力最強?

勉強可愛 這麼多期看下來,感覺還沒法看出來誰最後可以成為冠軍,但目前從節目和嘉賓以及選手的熱度來看,都是已經共贏了,張藝興在節目上向大家展示了舞蹈實力,同理其他三位隊長,也獲得了更多的熱度,一些跳的好的卻不知名的選手也獲得了熱度。冠軍是誰,其實還好,對於節目組來說,是要熱度,而你更重要的是獲得足夠多...