How to split columns without knowing how many columns will be generated in Python?

Viewed 172

I have a Dataframe with thousands of lines:

 Column1            Column2 
someString   String1;String2;String3
someString      String4;String5

I want to split column 2 in new columns by using ";" as a separator.

  Column1            Column2     Column3  Column4 Column5 
someString           String1     String3  String2
someString           String4     String5

While looking in the documentation, I tried a method:

df[['Subcolumn1','Subcolumn2','Subcolumn3','Subcolumn4']] = df.Column2.str.split(";",expand=True)

I had the ValueError: Columns must be same length as key

I can't predict how many columns will be created because I have thousands of lines and each line contains a different number of strings to be separated.

So I'm looking for a more "dynamic" way to generate these columns in order to avoid these value errors.

2 Answers

Try a solution using merge:

col2_expanded = df.Column2.str.split(";",expand=True)
df = pd.merge(df.col1, col2_expanded, left_index=True, right_index=True)

Good luck

tmp = pd.concat( [df['Column1'], df['Column2'].str.split(';', expand=True).add_prefix('SubColumn')], axis=1 )
print(tmp)

Prints:

      Column1 SubColumn0 SubColumn1 SubColumn2
0  someString    String1    String2    String3
1  someString    String4    String5       None
Related