Posts

Showing posts with the label Pandas

Extract Tables from a Web Page in Python

import pandas as pd xfiles = pd.read_html("https://en.wikipedia.org/wiki/List_of_The_X-Files_episodes") print(xfiles) # prints all the tables on the page as list print(type(xfiles)) print(len(xfiles)) print(xfiles[1]) # table of season 1 print(xfiles[12]) # table of season 10

Read CSV from a Web Page

import pandas as pd # the link is taken from right-clicking the csv file on a web page and 'copy link address' footballData = pd.read_csv("https://datahub.io/sports-data/english-premier-league/r/season-1819.csv") # print(footballData) # renaming two columns names; the inplace changes the original footballData.rename(columns={"FTHG": "Home", "FTAG": "Abroad"}, inplace=True) print(footballData)

Creating Pandas DataFrames

 Creating a DataFrame by using a dictionary: familyDict = {     "Names" : [ "Rehman" , "Wadud" , "Wahab" , "Aasia" , "Memoona" ],     "Age" : [ 35 , 26 , 30 , 24 , 39 ],     "Qualification" : [ "Pharm-D" , "B-Tech" , "Civil" , "BA" , "Islamyat" ],     "Relation" : [ "Son" , "Son" , "Son" , "Daughter" , "Daughter" ],     "Status" : [ "Married" , "Married" , "Married" , "Married" , "Married" ]     } import pandas as pd family_table = pd . DataFrame ( familyDict ) family_table . index = [ 1 , 2 , 3 , 4 , 5 ] # changes the index pattern of the table print ( family_table ) Creating a DataFrame by importing a CSV file using Pandas import pandas as pd notes = pd . read_csv ( "CSV-notes.csv" ) print ( notes )