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

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

根據(jù) Groupby 函數(shù)的輸出命名 Pandas 數(shù)據(jù)框

根據(jù) Groupby 函數(shù)的輸出命名 Pandas 數(shù)據(jù)框

一只萌萌小番薯 2021-09-11 16:36:44
我有一個數(shù)據(jù)集,其中包含在多個賽季的大量足球比賽中拍攝的所有鏡頭。我編寫了以下腳本來為每個比賽和相應(yīng)的賽季制作子集。import pandas as pdimport csvshots = pd.read_csv("C:/Users/HJA/Desktop/Betting/understatV0.01/shots.csv", encoding='iso-8859-1')shots_premier_league = shots.groupby(['Competition']).get_group('Premier_League')shots_bundesliga = shots.groupby(['Competition']).get_group('Bundesliga')shots_la_liga = shots.groupby(['Competition']).get_group('La_Liga')shots_ligue_1 = shots.groupby(['Competition']).get_group('Ligue_1')shots_serie_a = shots.groupby(['Competition']).get_group('Serie_A')一切順利,直到這一點。但是,現(xiàn)在我想將每個比賽細分為每個賽季的樣本。我使用以下腳本(在這種情況下,我以英超聯(lián)賽為例:shots_premier_league_2014 = shots_premier_league.groupby(['Season']).get_group('2014')shots_premier_league_2015 = shots_premier_league.groupby(['Season']).get_group('2015')shots_premier_league_2016 = shots_premier_league.groupby(['Season']).get_group('2016')shots_premier_league_2017 = shots_premier_league.groupby(['Season']).get_group('2017')shots_premier_league_2018 = shots_premier_league.groupby(['Season']).get_group('2018')這導(dǎo)致以下錯誤:我 100% 確定 2014 年是一個實際值。另外,如何編寫一個以pandas數(shù)據(jù)框的名義自動包含比賽和季節(jié)的函數(shù)?
查看完整描述

1 回答

?
繁星淼淼

TA貢獻1775條經(jīng)驗 獲得超11個贊

我認為問題是2014整數(shù),所以需要刪除'':


.get_group(2014)

但這里更好的是 create dictionary of DataFrameslike,因為不推薦使用全局變量:


dfs = dict(tuple(shots_premier_league.groupby(['Season'])))

然后通過鍵選擇每個數(shù)據(jù)幀,例如:


print (dfs[2014])

print (dfs[2015])

如何編寫一個以熊貓數(shù)據(jù)框的名義自動包含比賽和季節(jié)的函數(shù)?


dfs = dict(tuple(shots_premier_league.groupby(['Competition','Season'])))

print (dfs[('Bundesliga', 2014)])

如果要按字符串選擇:


d = dict(tuple(df.groupby(['Competition','Season'])))

#python 3.6+ solution with f-strings

dfs = {f'{k1}_{k2}' :v for (k1, k2), v in d.items()}

#python bellow

#dfs = {'{}_{}'.format(k1, k2) :v for (k1, k2), v in d.items()}

print (dfs['Bundesliga_2014'])

如果想查看數(shù)據(jù)的所有鍵:


print (dfs.keys())


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

添加回答

舉報

0/150
提交
取消
微信客服

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

幫助反饋 APP下載

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

公眾號

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