<jupyter_start><jupyter_text>Pandas-------- <NAME>   Loading in Data<jupyter_code>import pandas as pd
##-- For tabular data..
import numpy as np
!wget https://raw.githubusercontent.com/gauravpks/ml-repo/master/DataSet/sbux.csv
df = pd.read_csv('sbux.csv')
# using the URL directly works too!
df = pd.read_csv('https://raw.githubusercontent.com/gauravpks/ml-repo/master/DataSet/sbux.csv')
type(df)
!head sbux.csv<jupyter_output>date,open,high,low,close,volume,Name
2013-02-08,27.92,28.325,27.92,28.185,7146296,SBUX
2013-02-11,28.26,28.26,27.93,28.07,5457354,SBUX
2013-02-12,28.0,28.275,27.975,28.13,8665592,SBUX
2013-02-13,28.23,28.23,27.75,27.915,7022056,SBUX
2013-02-14,27.765,27.905,27.675,27.775,8899188,SBUX
2013-02-15,27.805,27.85,27.085,27.17,18195730,SBUX
2013-02-19,27.18,27.305,27.01,27.225,11760912,SBUX
2013-02-20,27.3,27.42,26.59,26.655,12472506,SBUX
2013-02-21,26.535,26.82,26.26,26.675,13896450,SBUX<jupyter_text>Stock Prices for StarBux<jupyter_code>df.head()
df.head(10)
df.tail()
df.info()<jupyter_output><class 'pandas.core.frame.DataFrame'>
RangeIndex: 1259 entries, 0 to 1258
Data columns (total 7 columns):
 #   Column  Non-Null Count  Dtype  
---  ------  --------------  -----  
 0   date    1259 non-null   object 
 1   open    1259 non-null   float64
 2   high    1259 non-null   float64
 3   low     1259 non-null   float64
 4   close   1259 non-null   float64
 5   volume  1259 non-null   int64  
 6   Name    1259 non-null   object 
dtypes: float64(4), int64(1), object(2)
memory usage: 69.0+ KB<jupyter_text>Selecting Rows and Columns<jupyter_code>df.columns
# columns can be assigned
df.columns = ['date', 'open', 'high', 'low', 'close', 'volume', 'name']
df.columns
df['open']
df[['open', 'close']]
type(df['open'])
## Series..for 1D
type(df[['open', 'close']])
## Data Frame from 2D
# To select row
df.iloc[0]
# Selects rows using index row
df.loc[0]
df2 = pd.read_csv('sbux.csv', index_col='date')
df2.head()
df2.loc['2013-02-08']
# To select row
df2.iloc[0]
type(df2.loc['2013-02-08'])
## TO select Multiple Rows
df[df['open'] > 64]
#Name is not start SBUX
## Seems in Bracket.. boolen is passed
df[df['name'] != 'SBUX']
df['name'] != 'SBUX'
type(df['name'] != 'SBUX')
# by the way, this works with Numpy too
import numpy as np
A = np.arange(10)
A
A[A % 2 == 0]
# don't want "objects" when we're doing math!
df.values
#Converting to numpy array as in ML we need series.. and not df
A = df[['open', 'close']].values
A
#gives a dypye array
type(A)
# write a dataframe to file
smalldf = df[['open', 'close']]
smalldf.to_csv('output.csv')
!head output.csv
# what if we don't want an index column?
smalldf.to_csv('output.csv', index=False)
!head output.csv<jupyter_output>open,close
27.92,28.185
28.26,28.07
28.0,28.13
28.23,27.915
27.765,27.775
27.805,27.17
27.18,27.225
27.3,26.655
26.535,26.675<jupyter_text>The apply() function<jupyter_code># an in python for loops are slow
def date_to_year(row):
  return int(row['date'].split('-')[0]) # Grabs the 0th element

df.apply(date_to_year, axis=1) ## Axis is required else will work column wize
df['year'] = df.apply(date_to_year, axis=1)
df.head()
##New Colum call year<jupyter_output><empty_output><jupyter_text>Basic Calculation<jupyter_code>np.mean(df['open'])
np.median(df['open'])
np.std(df['open'])
np.var(df['open'])
np.sqrt(np.var(df['open']))
np.max(np.var(df['open']))<jupyter_output><empty_output><jupyter_text>Plotting with Pandas<jupyter_code>## Plotting with panda makes easy as it gives an instane method for both row and column
df['open'].hist();
import matplotlib.pyplot as plt
#df['open'].hist();
plt.hist(df['open'].values);
df['open'].plot();
#Box Plot
df[['open', 'high', 'low', 'close']].plot.box();
# Summary
from pandas.plotting import scatter_matrix
scatter_matrix(df[['open', 'high', 'low', 'close']],
               alpha=0.2, figsize=(6, 6)); ## Alpha for transparancy<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.1822836661546339, 'fraction_numerical': 0.12365591397849462, 'mean_word_length': 4.771048744460857, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 25, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '326367', 'license': 'Unlicense', 'n_tokens_mistral': 1901, 'n_tokens_neox': 1619, 'n_words': 343, 'path': 'Part 0 - Welcome to Machine Learning A-Z/Pandas_Tutorial.ipynb', 'repo_name': 'gauravpks/ml-repo', 'size': '3914'}