Home > Software engineering >  Pandas ValueError: "Columns must be same length as key"
Pandas ValueError: "Columns must be same length as key"

Time:03-23

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

CodePudding user response:

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>.*)')
  • Related