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

Time:01-08

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'
driver.get(url)
driver.maximize_window()
# 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)

CodePudding user response:

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
  •  Tags:  
  • Related