<p>
Welcome to another episode of HPR I'm your host Enigma and today we are going to be talking
about one of my favorite python modules Pandas <br>
This will be the first episode in a series I'm naming: For The Love of Python. <br>
<br>
First we need to get the module <br>
pip or pip3 install pandas <br>
This will install numpy as well <br>
Pandas uses an object called a dataframe which is a two-dimensional data structure, <br>
i.e., data is aligned in a tabular fashion in rows and columns. Think of a spreadsheet type object in memory
<br> <br>
Today we are going to talk about: <br>
<b>1) Importing data from various sources</b><br>
Csv, excel, sql. More advance topics like Json covered in another episode. <br>
df = pd.read_csv('file name') <br>
<br>
<b>2) Accessing data by column names or positionally </b> <br>
print(df.head(5)) # print all columns only first 5 rows <br>
print(df.tail(5)) # print all columns only last 5 rows <br>
print(df.shape) # print number of rows and columns in dataframe <br>
print(df.columns) print column names <br>
print(df[0:1].head(5)) print first two columns first 5 values by column position <br>
print(df['field1].head(5)) print same column first five values by column name <br>
<br>
<b>3) Setting column types. </b> <br>
df['FieldName'] = df['FieldName'].astype(int) # sets column as interger <br>
df['FieldName'] = df['FieldName'].astype(str) # sets column to string <br>
df['DateColumn'] = pd.to_datetime(df['DateColumn']) # sets column to Datetime <br>
<br>
<br>
<b>4) Some basic filtering/manipulation of data. </b><br>
Splits string at the @ for one split next two lines create 2 columns that use the pieces. <br>
new = df2["Email"].str.split("@", n = 1, expand = True) <br>
df2["user"]= new[0] <br>
df2["domain"]= new[1] <br>
<br>
df['col'] = df['Office'].str[:3] # creates a new column grabing the first 3 positions of Office column <br>
df = df[df['FieldName'] != 0] # Only keep rows that have a FieldName value not equal to zero <br>
<br>
See example code that you can run at: <br>
<a href = 'https://github.com/eth0enigma/PandasExample'> Pandas Working example </a>
</p>