我對 python 還是個新手,并不完全確定解決這個問題的方法。我有一個關(guān)于視頻游戲的數(shù)據(jù)框,其中包含重要的標題、平臺、全球銷售和發(fā)布日期。有一些條目缺少發(fā)布日期。如果條目的全球銷售價值也非 0,我想用平臺的平均發(fā)布日期替換缺失值。我不完全確定如何構(gòu)建它以便它提取適當?shù)钠骄?,無論我是否需要嵌套循環(huán)等。請告訴我我是否在正確的軌道上或者我可以做些什么來合并這個如果您需要任何說明,謝謝! games.head() Name Platform Global_Sales Release_Date 0 Grand Theft Auto: San Andreas PS2 20.81 2004-10-26 1 Grand Theft Auto V PS3 20.30 2013-09-17 2 Grand Theft Auto V PS4 18.46 2014-11-18 3 Grand Theft Auto: Vice City PS2 16.15 2002-10-28 4 Grand Theft Auto V X360 15.85 2013-09-17 games.info() <class 'pandas.core.frame.DataFrame'> Int64Index: 28852 entries, 0 to 28851 Data columns (total 4 columns): Name 28852 non-null object Platform 28852 non-null category Global_Sales 16025 non-null float64 Release_Date 27757 non-null datetime64[ns] for date in games.Release_Date: if pd.isnull(date) and games.Global_Sales !=0: games.Release_Date = [mean Release_Year for appropriate Platform]我有另一個 df 的平均值:platform_means,取自拆分我的日期時間對象并找到我想要使用的平均年份值。 platform_means.head() Platform Release_Year 0 3DS 2012.282895 1 DC 2000.077778 2 DS 2007.654777 3 GB 1999.375000 4 GBA 2003.180401 所以這將是我想要的一個例子,希望它有所幫助。我可以使用 Release_Date 作為日期時間或 Release_Date,這是一個 int,具體取決于哪個更容易。我以前從未有過約會時間。從這樣的事情: games.head() Name Platform Global_Sales Release_Date 0 A PS2 20.81 2004-10-26 1 B GBA 20.30 nan 2 C PS4 00.00 nan 3 D PS2 nan nan 4 E X360 15.85 2013-09-17
2 回答

料青山看我應(yīng)如是
TA貢獻1772條經(jīng)驗 獲得超8個贊
以下可能是您正在尋找的內(nèi)容:
for index, row in games[games['Release_Date'].isnull()].iterrows(): games.loc[games.index == index, 'Release_Date'] = platform_means.loc[platform_means.Platform == row['Platform'],'Release_Year'].item()

慕碼人2483693
TA貢獻1860條經(jīng)驗 獲得超9個贊
我會嘗試使用該pd.where
方法。請參閱文檔。
games['Release_Date'].where(games['Release_Date'].isnull(), games.join(platform_means, on='Platform')['Release_Year'])
添加回答
舉報
0/150
提交
取消