Python Forum
Break Up Tuple List Into A Dataframe? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Break Up Tuple List Into A Dataframe? (/thread-9749.html)



Break Up Tuple List Into A Dataframe? - digitalmatic7 - Apr-26-2018

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]


RE: Break Up Tuple List Into A Dataframe? - mlieqo - Apr-26-2018

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.


RE: Break Up Tuple List Into A Dataframe? - digitalmatic7 - Apr-26-2018

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]