Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Loading a dataframe
#1
I am working on loading a dataframe and have generated a variable called body.
in Python, when I print(body) it looks like this -

Data1a
Dataab
Data1c

Data2a
Data2b
Data2c

...

Datana
Datanb
Datanc

I'd like to load this variable/list into a dataframe where each group of three populates the three columns of the frame like -

Headera Headerb Headerc
Data1a Data1b Data1c
Data2a Data2b Data2c
...
Datana Datanb Datanc

I'm pretty new to python and can't find (or know how to search correctly) an example of this kind of operation. Any guidance is appreciated!!!

Or in other words - is there a way to format a variable as shown into a list for dataframe loading?

from

Data1a
Dataab
Data1c

Data2a
Data2b
Data2c

to

[['Data1a', 'Data1b', Data1c'],['Data2a', 'Data2b', 'Data2c']]
Reply
#2
Please show python code that creates the variable body and prints it.
Need to understand what exactly the python input code is to translate the output to
Output:
[['Data1a', 'Data1b', Data1c'],['Data2a', 'Data2b', 'Data2c']]
Reply
#3
from bs4 import BeautifulSoup
import requests
source = requests.get('https://website.com').text
soup = BeautifulSoup(source, 'lxml')

test = soup.find('table', class_='wikitable sortable')

for table in test.find_all('td'):
    body = table.text
    print(body)
Reply
#4
Ok thanks but that still doesn't show what body contains
Does body look something like this
body = ('\nData1a\nDataab\nData1c\n \nData2a\nData2b\nData2c\n \n...\n \n'
        'Datana\nDatanb\nDatanc\n')

print(body)
Output:
Data1a Dataab Data1c Data2a Data2b Data2c ... Datana Datanb Datanc
Reply
#5
Thanks for the help but I ended up using pandas.read_html and it formatted it for me. Much easier solution. Appreciate the assistance!
Reply


Forum Jump:

User Panel Messages

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