Ad

Indexing By Character In A Column Of A Python/Pandas DataFrame

- 1 answer

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!

Ad

Answer

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.

Ad
source: stackoverflow.com
Ad