kriomant
kriomant

Reputation: 2326

How to make pandas.Series.str.split work with empty dataset

I have following code splitting one column with values of form 'a-b' into two ones with values 'a' and 'b':

df[['name', 'value']] = df['value'].str.split('-', n=1, expand=True)

It works fine until df is empty when I get "Columns must be same length as key".

It there way to handle this condition flawlessly, without explicit check for emptiness?

Upvotes: 1

Views: 1124

Answers (1)

jezrael
jezrael

Reputation: 863361

I think here is problem no separator in all values of value column, so output is only 1 column DataFrame:

df = pd.DataFrame({'value':['', 'ab']})

print ( df['value'].str.split('-', n=1, expand=True))
    0
0    
1  ab

If need always 2 columns DataFrame in output with filled first column add DataFrame.reindex with list=[1,0]:

df[['name', 'value']] = df['value'].str.split('-', n=1, expand=True).reindex([1,0], axis=1)
print (df)
  value  name
0         NaN
1    ab   NaN

If need to fill second column by data:

df = pd.DataFrame({'value':['', 'ab']})

df1 = pd.DataFrame(columns=[0,1])
df[['name', 'value']] = df['value'].str.split('-', n=1, expand=True).append(df1)
print (df)
  value name
0   NaN     
1   NaN   ab

Or change list to [0,1] in DataFrame.reindex:

df = pd.DataFrame({'value':['', 'ab']})

df[['name', 'value']] = df['value'].str.split('-', n=1, expand=True).reindex([0,1], axis=1)
print (df)
   value name
0    NaN     
1    NaN   ab

Upvotes: 2

Related Questions