Skip to content

Instantly share code, notes, and snippets.

View SolClover's full-sized avatar

SolClover SolClover

View GitHub Profile
@SolClover
SolClover / Imp_pandas.py
Created August 7, 2020 03:14
Python Pandas
import pandas as pd
@SolClover
SolClover / Art001_SASst001.sas
Last active August 8, 2020 03:53
Create SAS dataset from scratch
data new_ds;
input Col_A $ Col_B Col_C;
datalines;
X 2 3
Y 5 6
Z 12 13
;
run;
@SolClover
SolClover / Art001_Python_001a.py
Created August 7, 2020 03:29
Create Pandas DataFrame
# Initialise list of lists
my_data = [['X', 2, 3], ['Y', 5, 6], ['Z', 12, 13]]
# Create Pandas DataFrame
df = pd.DataFrame(my_data, columns = ['Col_A', 'Col_B', 'Col_C'])
# Print DataFrame
df
@SolClover
SolClover / Art001_Python_001b.py
Created August 8, 2020 02:32
Create Pandas DataFrame
# Initialise dictionary of lists
my_data = {'Col_A':['X', 'Y', 'Z'], 'Col_B':[2, 5, 12], 'Col_C':[3, 6, 13]}
# Create DataFrame
df = pd.DataFrame(my_data)
# Print DataFrame
df
@SolClover
SolClover / Art001_SASst002.sas
Created August 8, 2020 02:36
Read in CSV into SAS
proc import datafile="C:\temp\test.csv"
out=test_dataset
dbms=csv
replace;
getnames=yes;
run;
@SolClover
SolClover / Art001_Python_002.py
Last active August 8, 2020 03:45
Read in CSV to Pandas DataFrame
# Import csv into a new Pandas DataFrame
new_df=pd.read_csv('C:/temp/test.csv')
# You can also specify which columns you want to read in with usecols by either
# specifying their name or position
# Using column names
new_df=pd.read_csv('C:/temp/test.csv', usecols=['Name','Surname', 'Height', 'Age'])
# Using column positions
new_df=pd.read_csv('C:/temp/test.csv', usecols=[0,1,2,3])
@SolClover
SolClover / Art001_SASst003.sas
Created August 8, 2020 02:45
SAS datastep example
data new_ds;
set old_ds;
run;
@SolClover
SolClover / Art001_Python_003.py
Last active August 8, 2020 03:46
Copy Pandas DataFrame
# Create new DataFrame based on old DataFrame
# Note, in this example you will create a view.
# Hence, your future changes to new_df will affect old_df too
new_df=old_df
# To make an actual copy of a DataFrame use this.
# In this case, future changes to new_df will not have an impact on old_df
new_df=old_df.copy()
# Print top 5 records of your new DataFrame
@SolClover
SolClover / Art001_SASst004.sas
Created August 8, 2020 02:55
SAS filtering using where statement
/* Filter on one condition */
data my_new_ds;
set my_ds;
where Col_A>=6;
run;
/* Filter on multiple conditions using AND */
data my_ds;
set my_ds;
where Col_A>=6 and Col_B=5;
@SolClover
SolClover / Art001_Python_004.py
Created August 8, 2020 02:57
Filter Pandas DataFrame
# Filter on one condition
new_df=df[df['Col_A']>=6]
# Filter on multiple conditions using AND
new_df=df[(df['Col_A']>=6) & (df['Col_B']==5)]
# Filter on multiple conditions using OR
new_df=df[(df['Col_A']>=6) | (df['Col_B']==1)]
# Create a list and use isin to filter