Here is my code:
player2023[['First Name', 'Last Name']] = player2023.Player.str.split(n=2, expand=True)
I was told to do that, opposed to the following code:
player2023[['First Name', 'Last Name']] = player2023.Player.str.split(expand=True)
I keep getting the same error for both:
ValueError: Columns must be same length as key
The data sample of the Player
attribute is John Smith
CodePudding user response:
It looks like you are trying to split the values in the "Player" column of the player2023 DataFrame into two separate columns, "First Name" and "Last Name". The str.split() method can be used for this, but it looks like you are getting a ValueError.
One possible cause of this error is that the str.split() method is not splitting the values in the "Player" column as expected. This can happen if the number of words in each player's name is not the same. For example, if some player names have two words and others have three or more, then the str.split() method will not be able to split all of the values into two columns.
To avoid this error, you can use the str.split() method with the n parameter set to 1 instead of 2. This will split each player's name into two columns regardless of how many words are in the player's name. For example:
player2023[['First Name', 'Last Name']] = player2023.Player.str.split(n=1, expand=True)
Alternatively, you can use the str.split() method without the n parameter, which will split each player's name into as many columns as there are words in the player's name. For example:
player2023[['First Name', 'Last Name']] = player2023.Player.str.split(expand=True)
CodePudding user response:
If you are absolutely sure the column Player
has at least two words, and assuming the last word is what you want in the last name column, something like this will do the job:
import panda as pd
df = pd.DataFrame([
{'Player': 'John Doe'},
{'Player': 'Jane Doe Too'}
])
df[['First', 'Last']] = df.Player.str.rsplit(n=1, expand=True)
Which leaves df
like:
Player First Last
0 John Doe John Doe
1 Jane Joe Too Jane Doe Too