Skip to content Skip to sidebar Skip to footer

How To Make Different Length List To A Single Dataframe In Python?

Structure of my data is in this form. data1: ['https://www.fullstackpython.com/', ['https://www.fullstackpython.com/table-of-contents.html', 'https://www.fullstackpython.com/bl

Solution 1:

If the source data format is consistent with the example, the "rows" could be looped over in the following way, and then converted to a DataFrame.

In [1]: src = ['https://www.fullstackpython.com/',
   ...:  ['https://www.fullstackpython.com/table-of-contents.html',
   ...:   'https://www.fullstackpython.com/blog.html'],
   ...:  [['Introduction', 'Development Environments', 'Web Development ',
   ...:    'Web App Deployment', 'Data', ''],
   ...:   ['5 Years of Full Stack Python',
   ...:    'GitPython and New Git Tutorials ',
   ...:    'First Steps with GitPython']],
   ...:  [[0.0, 0.0, 0.25, 0.29, 0.25, 0.25],
   ...:   [0.0, 1.0, 0.19]]]

In [2]: site, pages, nested_titles, nested_values = src

In [3]: data = []
   ...: for page, titles, values in zip(pages, nested_titles, nested_values):
   ...:     for title, value in zip(titles, values):
   ...:         data.append((site, page, title, value))
   ...: df = pd.DataFrame(data, columns=['Site', 'Page', 'Title', 'Value'])

In [4]: df
Out[4]:
                               Site                                               Page                             Title  Value
0  https://www.fullstackpython.com/  https://www.fullstackpython.com/table-of-conte...                      Introduction   0.00
1  https://www.fullstackpython.com/  https://www.fullstackpython.com/table-of-conte...          Development Environments   0.00
2  https://www.fullstackpython.com/  https://www.fullstackpython.com/table-of-conte...                  Web Development    0.25
3  https://www.fullstackpython.com/  https://www.fullstackpython.com/table-of-conte...                Web App Deployment   0.29
4  https://www.fullstackpython.com/  https://www.fullstackpython.com/table-of-conte...                              Data   0.25
5  https://www.fullstackpython.com/  https://www.fullstackpython.com/table-of-conte...                                     0.25
6  https://www.fullstackpython.com/          https://www.fullstackpython.com/blog.html      5 Years of Full Stack Python   0.00
7  https://www.fullstackpython.com/          https://www.fullstackpython.com/blog.html  GitPython and New Git Tutorials    1.00
8  https://www.fullstackpython.com/          https://www.fullstackpython.com/blog.html        First Steps with GitPython   0.19

Post a Comment for "How To Make Different Length List To A Single Dataframe In Python?"