Search code examples
pythonpandaspandas-explode

Python Dataframe Explode Rows with multiple values


I am sorry to replicate the same question which was answered before but they don't seem to give me the desired outcome, maybe I missed something.

I have a subset of the Stack Overflow dataset like the following:

**tags                          time**
c#,winforms                     35
html,css,internet-explorer-7    855
c#,conversion,j#                472
c#,datetime                     556
c#,.net,datetime,timespan       1
php,security                    3
mysql                           5
codeigniter,routes              4
c#,progressbar                  4
.net,ide,linux,mono             2

And I want the output like following:

**tags                  time**
c#                      35
winforms                35
html                    855
css                     855
internet-explorer-7     855
c#                      472
conversion              472
j#                      472
c#                      556
datetime                556
c#                      1
.net                    1
datetime                1
timespan                1
php                     3
security                3
mysql                   5
codeigniter             4
routes                  4
c#                      4
progressbar             4
.net                    2
ide                     2
linux                   2
mono                    2

I have tried the following methods:

  • Option-1:
df.explode('tags')
  • Option-2:
df.set_index(['time']).tags.apply(pd.Series).stack().reset_index(name = 'tags').drop('level_1', axis = 1)

In both cases, I get the output the same as my dataframe without exploding. What am I doing wrong here?


Solution

  • From pandas docs pandas.DataFrame.explode

    specify a non-empty list with each element be str or tuple

    To use explode your 'tags' column needs to be a list type. Apply a function to convert your string tags separated by commas to a list then go with option 1 df.explode('tags')