Python Forum

Full Version: Break Up Tuple List Into A Dataframe?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
It's my first time working with a tuple list, and I've run into a very strange issue.

df = pd.DataFrame(tuple_list)
Most items in the tuple list have 3 elements (url, number, sample data). Some items only have a url, in this case it breaks the dataframe and tries to assign 1 letter of the url per column.

Here's the full code:

https://pastebin.com/rGsFvv9a

When I run it, this is what the output looks like:

[Image: As616ilDR__Z8CYsGkPanw.png]
I' am totally new to this, but isn't this happening because you actually don't have list of tuples, but list of tuples and strings? As I'm looking on the items that have only url f.e. ->
('url, '116019', 'sample data'),
 'url', 
('url, '210600', 'sample data'),
(this is an actual data from your testing variable I just removed the urls)
the middle item is string and if you change it to tuple:
('url',)
it looks OK.
Thanks for bringing that issue to light mlieqo

I get it working using this code:

[t if type(t) == tuple else (t,) for t in results]