Skip to content

Instantly share code, notes, and snippets.

@mobileappconsultant
Created December 4, 2018 15:25
Show Gist options
  • Save mobileappconsultant/5eb21950b4971943c19cce32610d2230 to your computer and use it in GitHub Desktop.
Save mobileappconsultant/5eb21950b4971943c19cce32610d2230 to your computer and use it in GitHub Desktop.
Playing around with Pandas Module
import pandas as pd
import sys
import os
clear = lambda: os.system("clear") #on Linux System
clear()
def processUserOptions(argument):
switchOptions = {
1: retrieveAndLoadCSVFileFromUrl(),
2: retrieveAndLoadCSVFileFromUrlAndSkipFirstColumn(),
3: retrieveAndLoadCSVFileFromUrlAndUseIDNUMColumnAsIndex(),
4: retrieveAndLoadCSVFileFromUrlAndReadFirstThreeColumns(),
5: retrieveAndLoadCSVFileFromUrlAndSkipSpecificColumnsByName(),
6: retrieveAndLoadCSVFileFromUrlAndSkipTheFirstColumnWhichIsUnamed_Method_Two(),
7: retrieveAndLoadCSVFileFromUrlAndReadTheFirstThreeRows()
}
return switchOptions.get(argument, "That Option is not Available")
def retrieveAndLoadCSVFileFromUrl():
url_csv = 'https://vincentarelbundock.github.io/Rdatasets/csv/boot/amis.csv'
df = pd.read_csv(url_csv)
return df
def retrieveAndLoadCSVFileFromUrlAndSkipFirstColumn():
url_csv = 'https://vincentarelbundock.github.io/Rdatasets/csv/boot/amis.csv'
df = pd.read_csv(url_csv, index_col=0)
df.head()
return df
def retrieveAndLoadCSVFileFromUrlAndUseIDNUMColumnAsIndex():
csv_url = 'http://vincentarelbundock.github.io/Rdatasets/csv/carData/MplsStops.csv'
df = pd.read_csv(csv_url, index_col='idNum')
df.iloc[:, 0:1].head(6)
return df
def retrieveAndLoadCSVFileFromUrlAndReadFirstThreeColumns():
cols = [2, 4, 6, 8]
csv_url = 'http://vincentarelbundock.github.io/Rdatasets/csv/carData/MplsStops.csv'
df = pd.read_csv(csv_url, usecols=cols)
df.head()
return df
def retrieveAndLoadCSVFileFromUrlAndSkipSpecificColumnsByName():
cols = ['idNum', 'date', 'problem', 'MDC', 'vehicleSearch']
csv_url = 'http://vincentarelbundock.github.io/Rdatasets/csv/carData/MplsStops.csv'
df = pd.read_csv(csv_url, index_col='idNum',
usecols=cols)
df.head()
return df
def retrieveAndLoadCSVFileFromUrlAndSkipTheFirstColumnWhichIsUnamed_Method_Two():
csv_url = 'http://vincentarelbundock.github.io/Rdatasets/csv/carData/MplsStops.csv'
cols = pd.read_csv(csv_url, nrows=1).columns
df = pd.read_csv(csv_url, usecols=cols[1:])
df.head()
return df
def retrieveAndLoadCSVFileFromUrlAndReadTheFirstThreeRows():
csv_url = 'http://vincentarelbundock.github.io/Rdatasets/csv/carData/MplsStops.csv'
cols = pd.read_csv(csv_url, nrows=1).columns
df = pd.read_csv(csv_url, usecols=cols[1:], nrows=3)
df.head()
return df
operator = ''
while True:
print("Enter 0 to quit program")
print("""1. Retrieve And Load CSV From A Url
\n2. Retrieve And Load CSV From A Url,but Skip Column 1
\n3. Retrieve And Load CSV From A Url And Use IDNUMColumn as Index
\n4. Retrieve And Load CSV From A Url And Read First 4 Columns
\n5. Retrieve And Load CSV From A Url And Skip Specific Columns
\n6.Retrieve And Load CSV From A Url And Skip The First Column Which Is Unamed In This File -> Method_Two
\n7.Retrieve And Load CSV From A Url And Read The First THREE ROWS\n""")
operator = input("Choose your operator by entering the corresponding number ")
if len(operator) > 0 and int(operator) == 0:
clear
sys.exit(0)
elif len(operator) == 0:
clear
print("Sorry you gotta type something")
continue
else:
clear
print(processUserOptions(int(operator)))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment