简体   繁体   中英

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!

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM