Sergio
Sergio

Reputation: 23

How to split one pandas text column into a fixed amount of columns

I have a dataframe with one text column separated by "/" and want to create several columns by splitting that one. I know I can achieve that by using df['col'].str.split('/ ', expand=True)]. However, in my case I want to create a fixed amount of columns no matter how many splits I get. For example, I would to create 4 columns out of the following df, filling the last ones with Nan if split does not return enough chunks:

    col1                             part_0  part_1  part2  part3
0  "a/b"            =>            0   "a"     "b"     Nan    Nan
1  "a/b/c"                        1   "a"     "b"     "c"    Nan  

I haven't found any solution that allows me to do this, as all of them create the number of columns based on the biggest split and that does not work for me. Can you help me? Thanks!

Upvotes: 2

Views: 251

Answers (1)

jezrael
jezrael

Reputation: 862851

Use DataFrame.reindex and DataFrame.add_prefix:

df = pd.DataFrame({'col':['a/b','a/b/c']})

N = 4
df1 = df['col'].str.split('/', expand=True).reindex(range(N), axis=1).add_prefix('part_')
print (df1)
  part_0 part_1 part_2  part_3
0      a      b   None     NaN
1      a      b      c     NaN

Upvotes: 1

Related Questions