Python pandas dataframe how to output th as a column and tr as the row



Quite new to python and trying to learn from different forums and such.
I am scraping using selenium and this time i wanted to get a table.
The scraping and the output works fine.But i would like to get titles as columns and the tr value as the row..
today my output looks like this

enter image description here

but i want the output like this..

enter image description here

This is the code

# Import Library
from selenium import webdriver
import pandas as pd
# Open Browser
driver = webdriver.Chrome(path')
# Get the  URL
url = 'give url'
# Read and Convert Web table into data frame
webtable_df = pd.read_html(driver.find_element_by_xpath('').get_attribute('outerHTML'))[0]
# Write() to excel file
webtable_df.to_excel (r'path', index = False, header=True)


Transpose the data matrix (row to column here)

>>> import pandas as pd
>>> df = pd.DataFrame({'a':[1,2,3], 'b':[4,5,6]})
>>> df
   a  b
0  1  4
1  2  5
2  3  6
>>> df.T # there is df.transpose() too.
   0  1  2
a  1  2  3
b  4  5  6

Answered By – Vicrobot

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. Accept Read More