<jupyter_start><jupyter_text>Table of Contents 1&nbsp;&nbsp;Creating a series2&nbsp;&nbsp;Creating a DataFrame3&nbsp;&nbsp;The Series4&nbsp;&nbsp;Aligned vectorized operations4.1&nbsp;&nbsp;automatic index alignment5&nbsp;&nbsp;Data Frame6&nbsp;&nbsp;Make changes to series and dataframes  Creating a series<jupyter_code>import pandas as pd
s = pd.Series(['banana', 42])
s
# manually assign index values to a series
# by passing a Python list
s = pd.Series(['<NAME>', 'Creator of Pandas'],
              index=['Person', 'Who'])
s<jupyter_output><empty_output><jupyter_text>Creating a DataFrame<jupyter_code>scientists = pd.DataFrame({
    'Name': ['<NAME>', '<NAME>'],
    'Occupation': ['Chemist', 'Statistician']
})
scientists
scientists = pd.DataFrame(
    'Name': ['<NAME>', '<NAME>'],
    'Occupation': ['Chemist', 'Statistician']
    },
    index=['<NAME>', '<NAME>'],
    columns=['Occupation', 'Born', 'Died', 'Age']
)

scientists
# ordered dict
from collections import OrderedDict
scientists = pd.DataFrame(OrderedDict([
('Name', ['<NAME>', '<NAME>']),
    ('Occupation', ['Chemist', 'Statistician']),
('Born', ['1920-07-25', '1876-06-13']),
('Died', ['1958-04-16', '1937-10-16']),
('Age', [37, 61])
])
)

scientists<jupyter_output><empty_output><jupyter_text>The Series<jupyter_code># create our example dataframe
# with a row index label
scientists = pd.DataFrame(
data={'Occupation': ['Chemist', 'Statistician'],
'Born': ['1920-07-25', '1876-06-13'],
'Died': ['1958-04-16', '1937-10-16'],
'Age': [37, 61]},
index=['<NAME>', '<NAME>'],
columns=['Occupation', 'Born', 'Died', 'Age'])
print(scientists)
# select by row index label
first_row = scientists.loc['<NAME>']
print(type(first_row))
print(first_row)
print(first_row.index)
print(first_row.values)
# keys is like index
print(first_row.keys())
# get the first index using an attribute
print(first_row.index[0])
# get the first index using a method
print(first_row.keys()[0])<jupyter_output>Occupation<jupyter_text>http://pandas.pydata.org/pandas-docs/version/0.20.3/generated/pandas.Series.html<jupyter_code># is ndarray-link

# get the 'Age' column
ages = scientists['Age']
print(ages)
print(ages.mean())
print(ages.min())
print(ages.max())
print(ages.std())
scientists = pd.read_csv('../data/scientists.csv')
scientists
ages = scientists['Age']
print(ages)
# mean of all ages
print(ages.mean())
print(ages[ages > ages.mean()])
print(ages > ages.mean())
print(type(ages > ages.mean()))
# get index 0, 1, 4, and 5
manual_bool_values = [True, True, False, False, True, True, False, True]
print(ages[manual_bool_values])<jupyter_output>0    37
1    61
4    56
5    45
7    77
Name: Age, dtype: int64<jupyter_text>Aligned vectorized operations<jupyter_code>print(ages + ages)
print(ages * ages)
print(ages + 100)
print(ages * 2)
# vectors with different lengths

print(ages + pd.Series([1, 100]))
# with other types, the shapes must match

import numpy as np
# this will cause an error
print(ages + np.array([1, 100]))<jupyter_output><empty_output><jupyter_text>automatic index alignment<jupyter_code># ages as they appear in the data
print(ages)
rev_ages = ages.sort_index(ascending=False)
print(rev_ages)
# reference output to show index label alignment
print(ages * 2)
# note how we get the same values
# even though the vector is reversed
print(ages + rev_ages)<jupyter_output>0     74
1    122
2    180
3    132
4    112
5     90
6     82
7    154
Name: Age, dtype: int64<jupyter_text>Data Frame<jupyter_code># boolean vectors will subset rows
print(scientists[scientists['Age'] > scientists['Age'].mean()])
# 4 values passed as a bool vector
# 3 rows returned
print(scientists.loc[[True, True, False, True]])<jupyter_output>Name        Born        Died  Age    Occupation
0  <NAME>  1920-07-25  1958-04-16   37       Chemist
1     <NAME>  1876-06-13  1937-10-16   61  Statistician
3        <NAME>  1867-11-07  1934-07-04   66       Chemist<jupyter_text>Make changes to series and dataframes<jupyter_code># add a column
print(scientists['Born'].dtype)
print(scientists['Died'].dtype)
# format born as a datetime
born_datetime = pd.to_datetime(scientists['Born'], format='%Y-%m-%d')
print(born_datetime)
# format the 'Died' column as a datetime
died_datetime = pd.to_datetime(scientists['Died'], format='%Y-%m-%d')
scientists['born_dt'], scientists['died_dt'] = (born_datetime,
                                                died_datetime)
print(scientists.head())
# all the current columns in our data
print(scientists.columns)
# drop the shuffled age column
# you provide the axis=1 argument to drop column-wise
scientists_dropped = scientists.drop(['Age'], axis=1)
# columns after dropping our column
print(scientists_dropped.columns)<jupyter_output>Index(['Name', 'Born', 'Died', 'Occupation', 'born_dt', 'died_dt'], dtype='object')
{'fraction_non_alphanumeric': 0.1565362198168193, 'fraction_numerical': 0.04454621149042465, 'mean_word_length': 5.248374512353706, 'pattern_counts': {'":': 0, '<': 43, '<?xml version=': 0, '>': 47, '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': '8', 'dir': 'jupyter-structured-clean-dedup', 'id': '390612', 'license': 'MIT', 'n_tokens_mistral': 1831, 'n_tokens_neox': 1664, 'n_words': 426, 'path': 'old_stuff/class_notes/01-02-pandas_data_structures.ipynb', 'repo_name': 'chendaniely/2018-10-python1', 'size': '4937'}