What can we use in alternative option of explode() in pandas?

Viewed 1603

I am trying to run python script in which I am using explode() to split row into multiple rows but the condition is this we can use explode() in the higher version of pandas means pandas version should be greater than or equal to '0.25.0'

So in higher version of pandas explode() is working fine. I used below code:

columns = ['rule_id', 'applied_sql_function1', 'input_condition', 'input_value', 'and_or_not_oprtor', 'output_condition', 'priority_order','comb_fld_order']

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())

I am splitting comb_fld_order column.

I need some alternative solution so that new logic can give the same result like explode() in the lower version of pandas. Can someone please help me how to do this?

df1:

rule_id    priority_order    comb_fld_order   
R162       2.3               1
R162       2.3.1             1
R162       2.6               2
R162       2.6.1             2
R162       3.0.4             3.2,3.1,3

Expected Output:

df1:

rule_id    priority_order    comb_fld_order   
R162       2.3               1
R162       2.3.1             1
R162       2.6               2
R162       2.6.1             2
R162       3.0.4             3.2
R162       3.0.4             3.1
R162       3.0.4             3
3 Answers

Use DataFrame.stack after Series.str.split, then remove last level of MultiIndex by first Series.reset_index and by second convert Series to DataFrame:

df_main1 = (df_main1.set_index(['rule_id','priority_order'])['comb_fld_order']
                    .astype(str)
                    .str.split(',', expand=True)
                    .stack()
                    .reset_index(level=-1, drop=True)
                    .reset_index(name='comb_fld_order')
            )

print (df_main1)
  rule_id priority_order comb_fld_order
0    R162            2.3              1
1    R162          2.3.1              1
2    R162            2.6              2
3    R162          2.6.1              2
4    R162          3.0.4            3.2
5    R162          3.0.4            3.1
6    R162          3.0.4              3

Please try:

    df = df.set_index(['rule_id','priority_order']).comb_fld_order.apply(pd.Series).stack().reset_index(level=0).rename(columns={0:'comb_fld_order'}).reset_index()
    del df['level_1']

Hope this will work.

You could use itertools' product and chain.from_iterable to replicate explode :

from itertools import product, chain
outcome = chain.from_iterable(product([first], [second], last.split(',')) 
                              for first, second, last in df1.to_numpy()
                              )

pd.DataFrame(outcome, columns = df1.columns)

  rule_id   priority_order  comb_fld_order
0   R162    2.3              1
1   R162    2.3.1            1
2   R162    2.6              2
3   R162    2.6.1            2
4   R162    3.0.4            3.2
5   R162    3.0.4            3.1
6   R162    3.0.4            3
Related