Python Forum
Break Up Tuple List Into A Dataframe?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Break Up Tuple List Into A Dataframe?
#1
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]
Reply
#2
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.
Reply
#3
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]
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  using > < for tuple , list,... akbarza 3 1,648 Feb-05-2024, 01:18 PM
Last Post: deanhystad
Question mypy unable to analyse types of tuple elements in a list comprehension tomciodev 1 1,555 Oct-17-2023, 09:46 AM
Last Post: tomciodev
  for loops break when I call the list I'm looping through Radical 4 1,892 Sep-18-2023, 07:52 AM
Last Post: buran
  Change font in a list or tuple apffal 4 3,690 Jun-16-2023, 02:55 AM
Last Post: schriftartenio
  function returns dataframe as list harum 2 2,998 Aug-13-2022, 08:27 PM
Last Post: rob101
  search a list or tuple for a specific type ot class Skaperen 8 3,464 Jul-22-2022, 10:29 PM
Last Post: Skaperen
  Using .append() with list vs dataframe Mark17 7 15,148 Jun-12-2022, 06:54 PM
Last Post: Mark17
  why is my list a tuple CompleteNewb 7 3,575 Mar-17-2022, 10:09 PM
Last Post: CompleteNewb
  List of dataframe values beginning with x,y or z glidecode 3 2,717 Nov-08-2021, 10:16 PM
Last Post: glidecode
  Reading data to python: turn into list or dataframe hhchenfx 2 6,708 Jun-01-2021, 10:28 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020