Search code examples
pythonpandaspython-re

split record columns of the form win-lose-draw and sometimes NC into 4 separate columns


my dataframe has a column 'record' that most of the time looks like 10-3-0 but sometimes it also include the No Contest "35-11-2 (1 NC)"

I would like to split this column into 4 separate columns "win" "lose" "draw" "nc" with nc takes None as value when there is no indication of NC in the record column


Solution

  • df_test = pd.DataFrame(data={'records':["35-11-2 (1 NC)", "30-11-12 (2 NC)", "20-11-2"]})
    df_test['records'] = df_test['records'].str.replace(' \(', '-(', regex=True)
    print(df_test)
    df_test[['win', 'lose', 'draw', 'nc']] = df_test['records'].str.split('-', expand=True)
    print(df_test)