1

I am trying to run python script which I am using explode(). In my local it is working fine but when I am trying to run on server it is giving error.

I am using below code:

df_main1 = (df_main1.set_index(['rule_id', 'applied_sql_function1', 'input_condition', 'input_value', 'and_or_not_oprtor', 'output_condition', 'priority_order']).apply(lambda x: x.astype(str).str.split(',').explode()).reset_index())

Error I am getting:

("'Series' object has no attribute 'explode'", u'occurred at index comb_fld_order')
Shivam
  • 213
  • 5
  • 14
  • 1
    what is pandas version in the server? looks like the server has a lower version of pandas than 0.25 – anky Jun 05 '20 at 06:43
  • The linked question is not an answer to this one. The correct answer is jezrael's and anky's: the version of pandas on the server is too old to use `explode()`. – Jonathan E. Landrum Jul 20 '20 at 16:41

3 Answers3

1

Problem is different versions of pandas, because Series.explode working in later versions only:

New in version 0.25.0.

jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
1

Try:

df_main1 = (df_main1.set_index(['rule_id', 'applied_sql_function1', 'input_condition', 'input_value', 'and_or_not_oprtor', 'output_condition', 'priority_order'])[col].str.split(',', expand=True).stack()

Where col is the name of the string column, which you wish to split and explode.

Generally expand will do the horizontal explode, while stack will move everything into one column.

Grzegorz Skibinski
  • 12,624
  • 2
  • 11
  • 34
0

I used to below code to get rid off from explode():

df_main1 = (df_main1.set_index(['rule_id', 'applied_sql_function1', 'input_condition', 'input_value', 'and_or_not_oprtor', 'output_condition', 'priority_order'])['comb_fld_order']
                    .astype(str)
                    .str.split(',', expand=True)
                    .stack()
                    .reset_index(level=-1, drop=True)
                    .reset_index(name='comb_fld_order'))
Shivam
  • 213
  • 5
  • 14
  • Cools - and it works, right? In such case - my suggestion would be to mark my post as an answer - cause it encapsulated the idea that solved your problem (SO isn't always about wrapping up the solution, it's meant to give a direction too) – Grzegorz Skibinski Jun 05 '20 at 11:52