繁体   English   中英

使用多个条件语句创建新列

[英]Using multiple conditional statements to make new columns

我有一个数据框train_df

   driverID  raceID    race_date       win
0   1          2       2018-04-20       1
1   1          3       2018-06-20       0
2   1          4       2018-08-20       0
3   1          5       2018-10-20       1
4   1          6       2019-01-20       1
5   2          2       2018-04-20       0
6   2          3       2018-06-20       1
7   2          4       2018-08-20       1
8   2          5       2018-10-20       0
9   2          6       2019-01-20       0

有两名车手,不同日期的多场比赛和一个获胜栏,表明车手是否赢得了比赛。 driverID 是每个车手的唯一标识符,我试图计算他们的获胜次数,从他们第一次获胜开始的月份数,以及最后一列(World Class (WC)),即识别基于世界级的车手根据我对世界级的定义(即在前六个月的比赛中至少赢得两场胜利的车手)。 所以生成的数据框应该是这样的:

   driverID  raceID    race_date       win      win_total     months_from_first  WC
0   1          2       2018-04-20       1          1                  0          n 
1   1          3       2018-06-20       0          1                  2          n
2   1          4       2018-08-20       0          1                  4          n 
3   1          5       2018-10-20       1          2                  6          y
4   1          6       2019-01-20       1          3                  9          y
5   2          2       2018-04-20       0          0                  0          n
6   2          3       2018-06-20       1          1                  0          n
7   2          4       2018-08-20       1          2                  2          y
8   2          5       2018-10-20       0          2                  4          y
9   2          6       2019-01-20       0          2                  7          y

我正在努力解决我的前两个目标并四处搜索,但我找不到任何类似的问题,而且我很难找到类似的问题。 因此,非常感谢任何帮助,谢谢.. 我相信我可以解决最后一个问题(即,一旦我得到帮助,就制作 WC 专栏)。

一种选择是按“ groupby ”分组并应用自定义 function 进行各种操作以获得所需的数据。

def get_data(x):
    # find the cumulative sum of wins
    x['win_total'] = x['win'].cumsum()
    # get the difference in months between race date and the first win
    x['months_from_first'] = (x['race_date'] - x.loc[x['win']==1, 'race_date'].iloc[0])//np.timedelta64(1, 'M')
    # fill race dates before first win with 0
    x.loc[x['months_from_first'] < 0, 'months_from_first'] = 0 
    return x

df['race_date'] = pd.to_datetime(df['race_date'])
df = df.groupby('driverID').apply(get_data)

# define WC using 2 wins as a cutoff point
df['WC'] = np.where(df['win_total'] >= 2, 'y', 'n')

Output:

   driverID  raceID  race_date  win  win_total  months_from_first WC
0         1       2 2018-04-20    1          1                  0  n
1         1       3 2018-06-20    0          1                  2  n
2         1       4 2018-08-20    0          1                  4  n
3         1       5 2018-10-20    1          2                  6  y
4         1       6 2019-01-20    1          3                  9  y
5         2       2 2018-04-20    0          0                  0  n
6         2       3 2018-06-20    1          1                  0  n
7         2       4 2018-08-20    1          2                  2  y
8         2       5 2018-10-20    0          2                  4  y
9         2       6 2019-01-20    0          2                  7  y

要获取前两列:

import pandas as pd
df['race_date'] = pd.to_datetime(df['race_date'])
df['total']=df.groupby('driverID')['win'].cumsum()
df['month']= df['race_date'].dt.to_period('M') - df.groupby('driverID')['race_date'].transform('min').dt.to_period('M')
df

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM