SimplyAbstruse
SimplyAbstruse

Reputation: 3

Pandas ValueError: "Columns must be same length as key"

I am using Jupyter Labs for this. I'm trying to split a column into two and receiving the error "Columns must be same length as key"

dataframe image

Code for the first split:

two_new_columns = ['Rank', 'Title']
df[two_new_columns] = df['Rank & Title'].str.split('.', 1, expand=True)

df

full error thrown image

Using str.extract, I get the correct column headers, but no rows to follow. str.extract image

Any suggestions as to how to fix this error? Expected Output

Upvotes: 0

Views: 6906

Answers (1)

jezrael
jezrael

Reputation: 862481

Here . is regex special character, so add regex=False for not processing it like regex patatern:

df[two_new_columns] = df['Rank & Title'].str.split('.', 1, expand=True, regex=False)

Or escape regex by \:

df[two_new_columns] = df['Rank & Title'].str.split(r'\.', 1, expand=True)

Alternative solution:

df1 = df['Rank & Title'].str.extract(r'(?P<Rank>\d+)\.(?P<Title>.*)')

Upvotes: 2

Related Questions