1

I am working on a project in which I scraped NBA data from ESPN and created a DataFrame to store it. One of the columns of my DataFrame is Team. Certain players that have been traded within a season have a value such as LAL/LAC under team, rather than just having one team name like LAL. With these rows of data, I would like to make 2 entries instead of one. Both entries would have the same, original data, except for 1 of the entries the team name would be LAL and for the other entry the team name would be LAC. Some team abbreviations are 2 letters while others are 3 letters.

I have already managed to create a separate DataFrame with just these rows of data that have values in the form team1/team2. I figured a good way of getting the data the way I want it would be to first copy this DataFrame with the multiple team entries, and then with one DataFrame, keep everything in the Team column up until the /, and with the other, keep everything in the Team column after the slash. I'm not quite sure what the code would be for this in the context of a DataFrame. I tried the following but it is invalid syntax:

first_team =  first_team['TEAM'].str[:first_team[first_team['TEAM'].index("/")]]

where first_team is my DataFrame with just the entries with multiple teams. Perhaps this can give you a better idea of what I'm trying to accomplish!

Thanks in advance!

1 Answers1

0

You're probably better off using split first to separate the teams into columns (also see Pandas DataFrame, how do i split a column into two), something like this:

d = pd.DataFrame({'player':['jordan','johnson'],'team':['LAL/LAC','LAC']})
pd.concat([d, pd.DataFrame(d.team.str.split('/').tolist(), columns =  ['team1','team2'])], axis = 1)

    player     team team1 team2
0   jordan  LAL/LAC   LAL   LAC
1  johnson      LAC   LAC  None

Then if you want separate rows, you can use append.

Community
  • 1
  • 1
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • This worked out great, thank you! I'm a bit confused, however, about how to separate the rows now. I have my dataframe with team, team1, team2, and team3 columns (some players actually were traded twice and as a result were on 3 teams). How can I use append to now have multiple entries for each team (i.e. if a player's original team value was lal/lac, that entire row should be removed and replaced with 2 separate rows, both keeping the original row/column data(there are over 10 columns in my dataframe) but one having a team value of lal and the other having lac. – Javesh Gandhi Jan 13 '16 at 18:39