我有一個數據集,如下所示,包括每場比賽的球員屬性。數據集採用Pandas Dataframe格式,並按照每個characterId的日期按降序排列。如何根據另一列和一個鍵在數據框中設置新列的值?
characterId date kills deaths matchResult
200 3-5-2014-22:30:10 10 12 0
300 4-4-2014-23:30:10 10 13 1
400 1-2-2014-17:30:10 9 10 1
201 3-5-2014-22:20:05 11 16 0
301 1-4-2014-20:20:05 18 15 1
我想補充一點,包含的最後一場比賽的matchResult
值的新列(nextResult
)。新的數據框中應如下所示:
characterId date kills deaths matchResult nextResult
200 3-5-2014-22:30:10 10 12 0 nan
300 4-4-2014-23:30:10 10 13 1 0
400 1-2-2014-17:30:10 9 10 1 1
201 3-5-2014-22:20:05 11 16 0 nan
301 1-4-2014-20:20:05 18 15 1 0
感謝您的解決方案。我試過了,但是它顯示如下錯誤: 'features.shift(1)['matchResult']。where(features.characterId.diff()。fillna(0).ge(0)) File「 C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ pandas \ core \ series.py「,行1459,差異 result = algos.diff(_values_from_object(self),句點) 文件」C:\ ProgramData \ Anaconda2 \ lib \ site-packages \ pandas \ core \ algorithms.py「,行1251,in diff out_arr [res_indexer] = arr [res_indexer] - arr [lag_indexer] TypeError:不支持的操作數類型爲 - :'str '和'str'' – YNr
你是CharacterId的總是整數還是可以有一些字母呢? –
'characterId'是一個字符串。 – YNr