<jupyter_start><jupyter_text>Data Analysis Pandas This is a module that's used for a lot of scientific and data science projects and is a good way to get familiar withmodules an python sytle syntax. Good resources are:    - Pandas for Everyone - <NAME>        - https://github.com/chendaniely/pandas_for_everyone         - Video https://www.youtube.com/watch?v=oGzU688xCUs (3:45:16)     - Python for Data Analysis - Mckinney https://github.com/wesm/pydata-book     - Documentation https://pandas.pydata.org/pandas-docs/stable/    - Python Data Science handbook https://github.com/jakevdp/PythonDataScienceHandbook<jupyter_code>!pip install pandas
import pandas as pd
#check the methods 
dir(pd)
#verify that there's a read_csv and read_excel we'll use them next 
#use data https://github.com/chendaniely/pandas_for_everyone/tree/master/data
df = pd.read_csv('./data/gapminder.tsv', sep='\t')
df
df.shape
df.head()
df.tail()
df.describe
df.columns
df.dtypes
df.info()
df['country'].value_counts()
df['continent'].value_counts()
df['lifeExp'].value_counts()
df['lifeExp'].describe()
#finding specific rows 
df.loc[42,'country']
df.iloc[42,0]
#aggregation 
df.groupby('year')['lifeExp'].mean()
df.groupby(['year','continent','lifeExp', 'gdpPercap']).max()
df.groupby('continent')['country'].nunique()
#https://pythontic.com/pandas/dataframe-computations/correlation
df.corr(method="pearson")
df.corr(method='kendall')<jupyter_output><empty_output><jupyter_text>Scipy https://docs.scipy.org/doc/scipy/reference/stats.htmlthis is used for advanced statistics<jupyter_code>!pip install scipy<jupyter_output>Requirement already satisfied: scipy in c:\users\i080272\appdata\local\continuum\anaconda4\envs\pd\lib\site-packages (1.4.1)
Requirement already satisfied: numpy>=1.13.3 in c:\users\i080272\appdata\local\continuum\anaconda4\envs\pd\lib\site-packages (from scipy) (1.18.1)<jupyter_text>Some Plotting<jupyter_code>!pip install seaborn
import seaborn as sns
import matplotlib.pyplot as plt<jupyter_output><empty_output><jupyter_text>Built in data for seaborn - anscombe data https://en.wikipedia.org/wiki/Anscombe's_quartet<jupyter_code>anscombe = sns.load_dataset("anscombe")
anscombe
fig=plt.figure()
fig.suptitle("Data")
dataset_1 = anscombe[anscombe['dataset']=='I']
plt.plot(dataset_1['x'],dataset_1['y'],'o')<jupyter_output><empty_output><jupyter_text>Tips Data from Seaborn<jupyter_code>tips = sns.load_dataset("tips")
tips
fig= plt.figure()
axes1 = fig.add_subplot(1,1,1)
axes1.hist(tips['total_bill'], bins =10 ) 
axes1.set_title('Histogram of total Bill') ;
#Bivariate 
scatter_plot = plt.figure()
ax1 = scatter_plot.add_subplot(1,1,1)
ax1.scatter(tips['total_bill'], tips['tip'])
ax1.set_title('Total Bill vs tip')
ax1.set_xlabel('total bill')
ax1.set_ylabel('tip')
scatter_plot.show ;
#2D Density plot 
kde, ax = plt.subplots()
ax = sns.kdeplot(data=tips['total_bill'], 
data2 = tips['tip'],
shade=True)

## Hexbin plot 
hexbin = sns.jointplot(x='total_bill', y="tip", data = tips, kind = "hex")<jupyter_output><empty_output><jupyter_text>Example with Microbiome dataThis uses excel and goes through some basic data analysis. Good site on dealing with excel - https://www.pybloggers.com/2018/11/pandas-excel-tutorial-how-to-read-and-write-excel-files/ - https://cmdlinetips.com/2018/03/how-to-change-column-names-and-row-indexes-in-pandas/Data is from https://microbiomedb.org/mbio/app/record/sample/MBSMPL0020-4-1816 site<jupyter_code>%config IPCompleter.greedy=True
#https://stackoverflow.com/questions/45390326/how-to-get-autocomplete-in-jupyter-notebook-without-using-tab 
!pip install xlrd
#C:\DSCI521\Chapin\data\Student Datasets Microbiome Unit 2019-2020.xlsx
mb = pd.read_excel(r'./data/Student Datasets Microbiome Unit 2019-2020.xlsx')
mb
#C:\DSCI521\Chapin\data\Student Datasets Microbiome Unit 2019-2020.xlsx
mb = pd.read_excel(r'./data/Student Datasets Microbiome Unit 2019-2020.xlsx', sheet_name='Dataset 1')
mb
cols=[1,2,3]
mb = pd.read_excel(r'./data/Student Datasets Microbiome Unit 2019-2020.xlsx', sheet_name='Dataset 1', usecols = cols)
mb
#nice but column names are going to be cumbersome rename them to 'Site', 'Bacteria'
mb.columns = ['Site', 'Bacteria','Sample']
mb.head()
mb.describe()
mb.info()
mb.Site.unique() 
mb.Site.value_counts()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.14767441860465116, 'fraction_numerical': 0.032325581395348836, 'mean_word_length': 6.0163132137031, 'pattern_counts': {'":': 0, '<': 25, '<?xml version=': 0, '>': 26, 'https://': 13, 'lorem ipsum': 0, 'www.': 2, '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': '547067', 'license': 'MIT', 'n_tokens_mistral': 1735, 'n_tokens_neox': 1610, 'n_words': 295, 'path': '2_Pandas.ipynb', 'repo_name': 'mxch2/test2', 'size': '4305'}