我正在處理一個資料框,它有一個名為season(新創建,np.nan 填充)的列,另一列是match_id,它的值是這樣的: match 1 有match_id 1,match 2 有match_id 2,...,匹配 n有match_id n。這是板球(接近棒球)資料集,所以它是一個球一個球。1 場比賽最多有20 20 個回合(每個回合有6 個球)。所以match_id 1大約是從索引 0 到 240。然后match_id 2 大約是從索引 241 到 480。資料是逐球(1 排 1 球)/逐場比賽(約 240 排 1 場比賽)/逐季(1 個賽季約 14160 行)。
我的條件是,如果match_id是從1到59,則將2017放在這些季節列行中。
在我的資料集中match_id和其他列預先存在。我創建了 np.nan column season,現在我想填充它。
我的資料看起來像,
In[]: df_raw.head(6)
out[]:
season match_id inning batting_team bowling_team over ball
0 NaN 1 1 Sunrisers Hyderabad Royal Challengers Bangalore 1 1
1 NaN 1 1 Sunrisers Hyderabad Royal Challengers Bangalore 1 2
2 NaN 1 1 Sunrisers Hyderabad Royal Challengers Bangalore 1 3
3 NaN 1 1 Sunrisers Hyderabad Royal Challengers Bangalore 1 4
4 NaN 1 1 Sunrisers Hyderabad Royal Challengers Bangalore 1 5
5 NaN 1 1 Sunrisers Hyderabad Royal Challengers Bangalore 1 6
uj5u.com熱心網友回復:
或者使用loc功能:
df.loc[(df['match_id']<=59) & (df['match_id']>=1), 'season'] = 2017
請注意,由于season列包含 NaN,它將被存盤為浮點數。填寫完season值后,您可以將值轉換為整數
df['season'] = df['season'].astype('int')
uj5u.com熱心網友回復:
我將程序分為兩個步驟,但您也可以將兩者合并為一行。
首先檢查 match_id 是否在指定范圍內,然后根據條件使用所需的值進行覆寫。
df['season'] = df['match_id'].isin(range(1,60)
df['season'] = df['season'].apply(lambda x: 2017 if x else np.nan)
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/350805.html
