mercredi 12 mai 2021

Separate a column of strings that contains either one or two sets of parentheses

I have a df like so:

     Team, Conf, 20_rec
   0 Miami (FL) (ACC), None, 7-6
   1 UCF (AAC), None, 6-4

I want to separate Team and Conf to achieve the following:

     Team, Conf, 20_rec
   0 Miami (FL), ACC, 7-6
   1 UCF , AAC, 6-4

I know I can separate the bottom row with: df[['Team','Conf'] = df.Team.str.split('(', expand = True) I have tried to set up conditions and if statements to check if a row contained a given value, checked if a given team was contained within a list of exceptions, and tried to use reverse splits to capture the final parenthetical value, but nothing seems to work.

exceptions = ['Miami (FL) (ACC)', 'Miami (OH) (MAC)']
for exception in exceptions:
     if df[~df['Team'].str.contains(exception):
         df[['Team','Conf'] = df.Team.str.split('(', expand = True)
     else:
         df[['Team','Conf'] = df.Team.str.split('\) \(', expand = True)

Pointing to the line of the if statement, this returns:

ValueError: The truth value of a DataFrame is ambiguous. Us a.empty, a.bool(), a.item(),a.any(), or a.all().

I want to look at each row, not the collective rows, and also don't want to return True/False into the Conf column, but actually want a conference.

Apologies for not including more code samples; I have strayed so far from the original intention that I have no idea what is even close, but thought this was one of the closer attempts. Thanks!!

Aucun commentaire:

Enregistrer un commentaire