<jupyter_start><jupyter_text>Introduction to Pandas<NAME> (<EMAIL>)<jupyter_code>%matplotlib inline
import pandas as pd
import matplotlib as ml
import sys
import numpy as np
plt = ml.pyplot
ml.rcParams['figure.figsize'] = (10.0, 5.0)

print("Python version: {0}\n"
      "Pandas version: {1}\n"
      "Matplotlib version: {2}"
      .format(sys.version, pd.__version__, ml.__version__))<jupyter_output>Python version: 3.6.8 |Anaconda, Inc.| (default, Dec 30 2018, 01:22:34) 
[GCC 7.3.0]
Pandas version: 0.24.1
Matplotlib version: 3.0.2<jupyter_text>The basic data structures in Pandas  `DataFrame`<jupyter_code>dseries = {'math' : pd.Series([10,5,17], index=['bob', 'alice', 'dan']),
     'french' : pd.Series([7, 12, 15, 13], index=['dan', 'alice', 'bob', 'nancy'])}


df = pd.DataFrame(dseries)
df
type(df)<jupyter_output><empty_output><jupyter_text>`Series`<jupyter_code>df['math']
type(df['math'])  
df['math'] * 23
np.cos(df['math'])
s = pd.Series(np.random.randint(0, 10, 5))
s
s.sort_values()  # indices are kept!<jupyter_output><empty_output><jupyter_text>Examining a `DataFrame`<jupyter_code>df
df.dtypes
df.columns
df.shape<jupyter_output><empty_output><jupyter_text>Looking into the data<jupyter_code>df.head(2)
df.tail(2)
df.describe()<jupyter_output><empty_output><jupyter_text>Using `.loc[]`This one treats the input as label or "row-name".<jupyter_code>df.index

df.loc['alice']
type(df.loc['alice'])

df.index<jupyter_output><empty_output><jupyter_text>Accessing multiple rows/columns<jupyter_code>df.loc[['alice','bob'],['math']]<jupyter_output><empty_output><jupyter_text>Using `.iloc[]`<jupyter_code>df.iloc[0]
df.iloc[-1]<jupyter_output><empty_output><jupyter_text>Grouped operations<jupyter_code>df = pd.DataFrame({'location' : ['chambery', 'annecy', 'chambery', 'annecy',
                                 'annecy', 'chambery', 'annecy', 'chambery'],
                   'course' : ['math', 'math', 'french', 'french', 'french', 'french', 'french', 'math'],
                   'rate' : np.random.randint(20,size=8),
                   'time' : np.random.randint(3, size=8)})
df
df.groupby(['location']).groups
df.groupby(['location']).get_group('annecy')
df = pd.DataFrame({
    'event_id': [1, 1, 1, 2, 2, 3, 3, 3, 3],
    'n_hits': [23, 13, 14, 44, 23, 45, 1, 2, 4]
})
df
df.groupby('event_id').first()
for  name, group_data in df.groupby('event_id'):
    print(group_data, "\n")
df.groupby('event_id').aggregate([np.mean, np.median, sum, min, max])<jupyter_output><empty_output><jupyter_text>Plotting<jupyter_code>df = pd.DataFrame({'foo': np.cumsum(np.random.random(1000) - 0.5),
                   'bar': np.cumsum(np.random.random(1000) - 0.5),
                   'narf': np.cumsum(np.random.random(1000) - 0.5)})
df
df.plot()
df.hist();
plt.scatter(df['foo'], df['bar']);<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.19519094766619519, 'fraction_numerical': 0.03606789250353607, 'mean_word_length': 5.006369426751593, 'pattern_counts': {'":': 0, '<': 42, '<?xml version=': 0, '>': 42, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '10', 'dir': 'jupyter-structured-clean-dedup', 'id': '401759', 'license': 'MIT', 'n_tokens_mistral': 1228, 'n_tokens_neox': 1134, 'n_words': 193, 'path': 'pandas/Pandas_Warm_Up_M1.ipynb', 'repo_name': 'vuillaut/testgitpod', 'size': '2840'}