I found this example on another post, but its for R, I need it for Python in a pandas dataframe
Original post = Split string every n characters new column
Suppose I have a data frame like this with a string vector, var2
var1 | var2 |
---|---|
1 | abcdefghi |
2 | abcdefghijklmnop |
3 | abc |
4 | abcdefghijklmnopqrst |
What is the most efficient way to split var2 every n characters into new columns until the end of each string,
e.g if every 4 characters, the output would like look like this:
var1 | var2 | new_var1 | new_var2 | new_var3 | new_var4 | new_var5 |
---|---|---|---|---|---|---|
1 | abcdefghi | abcd | efgh | i | ||
2 | abcdefghijklmnop | abcd | efgh | ijkl | mnop | |
3 | abc | abc | ||||
4 | abcdefghijklmnopqrst | abcd | efgh | ijkl | mnop | qrst |
To make it more difficult, I don't know how big is the longest string in column, but I need the total column to be as many as needed based on the resulting N columns