<jupyter_start><jupyter_text>CH.5 Getting Started with Pandas  0. Import Packages<jupyter_code>import pandas as pd
from pandas import Series, DataFrame
import numpy as np<jupyter_output><empty_output><jupyter_text>1. Series<jupyter_code>obj = pd.Series([4,7,-5,3])
obj
print(obj.values)
print(obj.index)
obj2 = pd.Series([4,7,-5,3], index = ['d','b','a','c'])
obj2
print(obj2.index)
print(obj2['a'])
print(obj2['d'])
print(obj2[['c','a','d']])
print(obj2[obj2 > 0])
print(obj2 * 2)
print(np.exp(obj2))
print('b' in obj2)
print('e' in obj2)
sdata = {'Ohio' : 35000, "Texas" : 71000, "Oregon" : 16000}
obj3 = pd.Series(sdata)
obj3
states = ['Callifornia', 'Ohio', 'Oregon', 'Texas']
obj4 = pd.Series(sdata, index=states)
obj4
print(pd.isnull(obj4))
print(pd.notnull(obj4))
print(obj3 + obj4)<jupyter_output>Callifornia         NaN
Ohio            70000.0
Oregon          32000.0
Texas          142000.0
dtype: float64<jupyter_text>2. DataFrame<jupyter_code>data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'],
 'year': [2000, 2001, 2002, 2001, 2002, 2003],
 'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}
frame = pd.DataFrame(data)
frame
frame.head()
pd.DataFrame(data, columns=['year', 'state', 'pop'])
frame2 = pd.DataFrame(data, columns=['year', 'state', 'pop', 'debt'],
    index=['one', 'two', 'three', 'four',
    'five', 'six'])
frame2
print(frame2.columns)
print(frame2['state'])
print(frame2.year)
frame2.loc['three']
frame2['debt'] = 16.5
frame2['debt'] = np.arange(6.)
frame2
val = pd.Series([-1.2, -1.5, -1.7], index=['two', 'four', 'five'])
frame2['debt'] = val
frame2
frame2['eastern'] = frame2.state == 'Ohio'
frame2
del frame2['eastern']
frame2.columns
pop = {'Nevada': {2001: 2.4, 2002: 2.9},
    'Ohio': {2000: 1.5, 2001: 1.7, 2002: 3.6}}
frame3 = pd.DataFrame(pop)
frame3
frame3.T
pd.DataFrame(pop, index=[2001, 2002, 2003])
pdata = {'Ohio': frame3['Ohio'][:-1],
    'Nevada': frame3['Nevada'][:2]}
pd.DataFrame(pdata)
frame3.index.name = 'year'; frame3.columns.name = 'state'
frame3<jupyter_output><empty_output><jupyter_text>3. Index Object<jupyter_code>obj = pd.Series(range(3), index=['a', 'b', 'c'])
index = obj.index

print(index)
print(index[1:])
labels = pd.Index(np.arange(3))
labels
obj2 = pd.Series([1.5, -2.5, 0], index=labels)
obj2
obj2.index is labels
print(frame3)
print(frame3.columns)
print('Ohio' in frame3.columns)
print(2003 in frame3.index)
dup_labels = pd.Index(['foo', 'foo', 'bar', 'bar'])
dup_labels<jupyter_output><empty_output><jupyter_text>4. Essential Functionality<jupyter_code># Reindexing
obj = pd.Series([4.5, 7.2, -5.3, 3.6], index=['d', 'b', 'a', 'c'])
obj2 = obj.reindex(['a', 'b', 'c', 'd', 'e'])
obj3 = pd.Series(['blue', 'purple', 'yellow'], index=[0, 2, 4])
obj3.reindex(range(6), method='ffill')
frame = pd.DataFrame(np.arange(9).reshape((3, 3)),
    index=['a', 'c', 'd'],
    columns=['Ohio', 'Texas', 'California'])

frame
frame2 = frame.reindex(['a', 'b', 'c', 'd'])
frame2
states = ['Texas', 'Utah', 'California']
frame.reindex(columns=states)<jupyter_output><empty_output><jupyter_text>5. Dropping Entries from an Axis<jupyter_code>obj = pd.Series(np.arange(5.), index=['a', 'b', 'c', 'd', 'e'])
new_obj = obj.drop('c')
print(obj)
print(new_obj)
obj.drop(['d', 'c'])
data = pd.DataFrame(np.arange(16).reshape((4, 4)),
    index=['Ohio', 'Colorado', 'Utah', 'New York'],
    columns=['one', 'two', 'three', 'four'])
data
data.drop(['Colorado', 'Ohio'])
data.drop('two', axis=1)
data.drop(['two', 'four'], axis='columns')
obj.drop('c', inplace=True)
obj<jupyter_output><empty_output><jupyter_text>6. Indexing, Selection, and Filtering<jupyter_code>obj = pd.Series(np.arange(4.), index=['a', 'b', 'c', 'd'])
print('[1]', obj)
print('[2]', obj['b'])
print('[3]', obj[1])
print('[4]', obj[2:4])
print('[5]', obj[['b', 'a', 'd']])
print('[6]', obj[[1, 3]])
print('[7]', obj[obj < 2])
print('[8]', obj['b':'c'])
obj['b':'c'] = 5
obj
data = pd.DataFrame(np.arange(16).reshape((4, 4)),
    index=['Ohio', 'Colorado', 'Utah', 'New York'],
    columns=['one', 'two', 'three', 'four'])
print('[1]', data)
print('[2]', data['two'])
print('[3]', data[['three', 'one']])
print('[4]', data[:2])
print('[5]', data[data['three'] > 5])
print('[6]', data < 5)
data[data < 5] = 0
data
# Selection with loc and iloc
print(data.loc['Colorado', ['two', 'three']])
print(data.iloc[2, [3, 0, 1]])
print(data.iloc[2])
print(data.iloc[[1, 2], [3, 0, 1]])
print(data.loc[:'Utah', 'two'])
print(data.iloc[:, :3][data.three > 5])
# integer index
ser = pd.Series(np.arange(3.))
print(ser)
ser2 = pd.Series(np.arange(3.), index=['a', 'b', 'c'])
print(ser2[-1])
print(ser[:1])
print(ser.loc[:1])
print(ser.iloc[:1])<jupyter_output>2.0
0    0.0
dtype: float64
0    0.0
1    1.0
dtype: float64
0    0.0
dtype: float64<jupyter_text>7. Arithmetic and Data Alignment<jupyter_code>s1 = pd.Series([7.3, -2.5, 3.4, 1.5], index=['a', 'c', 'd', 'e'])
s2 = pd.Series([-2.1, 3.6, -1.5, 4, 3.1],
    index=['a', 'c', 'e', 'f', 'g'])

print(s1)
print(s2)
print(s1 + s2)
df1 = pd.DataFrame(np.arange(12.).reshape((3, 4)),
    columns=list('abcd'))
df2 = pd.DataFrame(np.arange(20.).reshape((4, 5)),
    columns=list('abcde'))

df2.loc[1, 'b'] = np.nan
print(df1)
print(df2)
print(df1 + df2)
df1.add(df2, fill_value=0)
1 / df1
df1.rdiv(1)
df1.reindex(columns=df2.columns, fill_value=0)<jupyter_output><empty_output><jupyter_text>8. Function Application and Mapping<jupyter_code>frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'),
    index=['Utah', 'Ohio', 'Texas', 'Oregon'])
frame
print(np.abs(frame))
f = lambda x: x.max() - x.min()
frame.apply(f)

frame.apply(f, axis='columns')
def f(x):
    return pd.Series([x.min(), x.max()], index=['min', 'max'])

frame.apply(f)
format = lambda x: '%.2f' % x
frame.applymap(format)
frame['e'].map(format)<jupyter_output><empty_output><jupyter_text>9. Sorting and Ranking<jupyter_code>obj = pd.Series(range(4), index=['d', 'a', 'b', 'c'])
obj.sort_index()
frame = pd.DataFrame(np.arange(8).reshape((2, 4)),
    index=['three', 'one'],
    columns=['d', 'a', 'b', 'c'])
frame.sort_index()
frame.sort_index(axis=1)
frame.sort_index(axis=1, ascending=False)
obj = pd.Series([4, 7, -3, 2])
obj.sort_values()
obj = pd.Series([4, np.nan, 7, np.nan, -3, 2])
obj.sort_values()
frame = pd.DataFrame({'b': [4, 7, -3, 2], 'a': [0, 1, 0, 1]})
frame
frame.sort_values(by='b')
frame.sort_values(by=['a', 'b'])
obj = pd.Series([7, -5, 7, 4, 2, 0, 4])
obj.rank()
obj.rank(method='first')
obj.rank(ascending=False, method='max')<jupyter_output><empty_output><jupyter_text>10. Axis Indexes with Duplicate Labels<jupyter_code>obj = pd.Series(range(5), index=['a', 'a', 'b', 'b', 'c'])

print(obj)
print(obj.index.is_unique)
print(obj['a'])
print(obj['c'])
df = pd.DataFrame(np.random.randn(4, 3), index=['a', 'a', 'b', 'b'])

print(df)
print(df.loc['b'])<jupyter_output>0         1         2
a -1.380917  0.358536 -0.195972
a  0.345989  1.435080 -2.013246
b  1.174895 -0.368298  1.222719
b  0.789628 -1.374116  0.178898
          0         1         2
b  1.174895 -0.368298  1.222719
b  0.789628 -1.374116  0.178898<jupyter_text>11. Summarizing and Computing Descriptive Statistics<jupyter_code>df = pd.DataFrame([[1.4, np.nan], [7.1, -4.5],
    [np.nan, np.nan], [0.75, -1.3]],
    index=['a', 'b', 'c', 'd'],
    columns=['one', 'two'])
print(df)
print(df.sum())
print(df.sum(axis='columns'))
print(df.mean(axis='columns', skipna=False))
print(df.idxmax())
print(df.cumsum())
print(df.describe())
obj = pd.Series(['a', 'a', 'b', 'c'] * 4)
obj.describe()<jupyter_output><empty_output><jupyter_text>12. Unique Values, Value Counts, and Membership<jupyter_code>obj = pd.Series(['c', 'a', 'd', 'a', 'a', 'b', 'b', 'c', 'c'])
obj
uniques = obj.unique()
uniques
print(obj.value_counts())
print(pd.value_counts(obj.values, sort=False))
mask = obj.isin(['b', 'c'])
mask
to_match = pd.Series(['c', 'a', 'b', 'b', 'c', 'a'])
unique_vals = pd.Series(['c', 'b', 'a'])
pd.Index(unique_vals).get_indexer(to_match)
data = pd.DataFrame({'Qu1': [1, 3, 4, 3, 4],
    'Qu2': [2, 3, 1, 2, 3],
    'Qu3': [1, 5, 2, 4, 4]})

data
result = data.apply(pd.value_counts).fillna(0)
result<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.24904966278356835, 'fraction_numerical': 0.06866952789699571, 'mean_word_length': 5.4372533543804265, 'pattern_counts': {'":': 0, '<': 53, '<?xml version=': 0, '>': 53, 'https://': 0, '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': '13', 'dir': 'jupyter-structured-clean-dedup', 'id': '388249', 'license': 'MIT', 'n_tokens_mistral': 3813, 'n_tokens_neox': 3552, 'n_words': 712, 'path': '01. Python_Tutorials/3_Getting_Started_with_Pandas.ipynb', 'repo_name': 'OH-Seoyoung/Data_Analysis_Club', 'size': '8155'}