-1

I have dataframe df_ip_month and am trying to split the column detail EOBs into multiple columns on the same row for each Account such that this

enter image description here

becomes this

enter image description here

The code I am trying to use is

df_ip_month[['eob1','eob2','eob3','eob4','eob5','eob6']] = df_ip_month['detail EOBs'].str.split(expand=True)

However, no output is generated, only the following error

ValueError: Columns must be same length as key

How come?

Here is the dataset

df_ip_month = pd.DataFrame({'Account': ['H5000570011700','H5000484349900','H5000500029400','H5000502860000','H5000631774400','H5000619680500',
                           'H5000587425100','H5000630746300','H5000632095500','H5000505467800','H5000558994900','H5000623617700',
                           'H5000539983300','H5000559033600','H5000570061901','H5000513787300','H5000562451100','H5000568554900'], 
               'detail EOBs': ['','','','','','5002','','5002 1442','','5003','','','','5002 3035 9932 3343 3021 2312','','','5003 3035 9932','']})
Gabe Verzino
  • 346
  • 1
  • 10
  • 1
    In all cases but one, you have fewer than 6 values. They cannot be assigned to six columns. You may get a better answer if you provide the data as text, not as an image. – DYZ Mar 06 '20 at 22:42
  • Please read: [How to make good reproducible pandas examples](https://stackoverflow.com/questions/20109391/how-to-make-good-reproducible-pandas-examples). We cannot recreate your problem if it is in image format. – Ukrainian-serge Mar 06 '20 at 23:02
  • Sorry about that. I'm pretty new to Stack. I included the data set per the pandas example for easier reproduction. Thanks for the tip! – Gabe Verzino Mar 07 '20 at 02:01

1 Answers1

0
df = df.set_index('Account')
df2 = df['detail_EOBs'].str.split(expand=True)
df.join(df2)

You can set your index on your Account column, split and expand your detail_EOBs column, then join them back together.

Ben Pap
  • 2,549
  • 1
  • 8
  • 17