第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python2.7 - Pandas 數(shù)據(jù)框按兩個標(biāo)準(zhǔn)分組

Python2.7 - Pandas 數(shù)據(jù)框按兩個標(biāo)準(zhǔn)分組

回首憶惘然 2021-12-29 10:56:19
假設(shè)我有一個 panadas DataFrame:import pandas as pddf = pd.DataFrame(columns=['name','time'])df = df.append({'name':'Waren', 'time': '20:15'}, ignore_index=True)df = df.append({'name':'Waren', 'time': '20:12'}, ignore_index=True)df = df.append({'name':'Waren', 'time': '20:11'}, ignore_index=True)df = df.append({'name':'Waren', 'time': '01:29'}, ignore_index=True)df = df.append({'name':'Waren', 'time': '02:15'}, ignore_index=True)df = df.append({'name':'Waren', 'time': '02:16'}, ignore_index=True)df = df.append({'name':'Kim', 'time': '20:11'}, ignore_index=True)df = df.append({'name':'Kim', 'time': '01:29'}, ignore_index=True)df = df.append({'name':'Kim', 'time': '02:15'}, ignore_index=True)df = df.append({'name':'Kim', 'time': '01:49'}, ignore_index=True)df = df.append({'name':'Kim', 'time': '01:49'}, ignore_index=True)df = df.append({'name':'Kim', 'time': '02:15'}, ignore_index=True)df = df.append({'name':'Mary', 'time': '22:15'}, ignore_index=True)df = df.drop(df.index[2])df = df.drop(df.index[7])我想name按連續(xù)索引(按 Pandas DataFrame 中的連續(xù)索引分組)對該框架進(jìn)行分組,然后對其進(jìn)行分組。所需的輸出將是這樣的分組:因此,行按行分組,name并且對于行,此連續(xù)增加的索引僅采用第一個和最后一個元素。我這樣試過: df.groupby(['name']).groupby(df.index.to_series().diff().ne(1).cumsum()).group 這只會引發(fā)錯誤: AttributeError: Cannot access callable attribute 'groupby' of 'DataFrameGroupBy' objects, try using the 'apply' method歡迎任何幫助!
查看完整描述

1 回答

?
慕妹3146593

TA貢獻(xiàn)1820條經(jīng)驗(yàn) 獲得超9個贊

你做錯了。當(dāng)您執(zhí)行df.groupby(['name']) 時(shí),它會返回不可調(diào)用的屬性groupby。你需要同時(shí)應(yīng)用它。



df.groupby(['name', df.index.to_series().diff().ne(1).cumsum()]).groups


Out: 

{('Kim', 2): [6, 7],

 ('Kim', 3): [9, 10, 11],

 ('Mary', 3): [12],

 ('Waren', 1): [0, 1],

 ('Waren', 2): [3, 4, 5]}


查看完整回答
反對 回復(fù) 2021-12-29
  • 1 回答
  • 0 關(guān)注
  • 224 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號