<jupyter_start><jupyter_text>Handling Missing Data아래와 같은 함수를 통해 결측치를 확인하고 대치할 수 있다.```pythondf.dropna(axis=0, how='any')df.fillna(0)df.fillna('string')df.isnull.sum()df.notnull.sum()```<jupyter_code>import pandas as pd
import numpy as np
df = pd.DataFrame([[np.nan, 2, np.nan, 0], [3, 4, np.nan, 1],
                   [np.nan, np.nan, np.nan, 5]],
                  columns=list('ABCD'))
df
df.dropna(axis=1, how='any')
df.dropna(axis=0, how='all')
df.fillna(0)
values = {'A': 0, 'B': 1, 'C': 2, 'D': 3}
df.fillna(value=values)
df.isnull().sum()
df.notnull().sum()<jupyter_output><empty_output><jupyter_text>Reshaping Data아래와 같은 식을 사용하여 DataFrame을 Reshape할 수 있다.적절히 활용하여 원하는 형태로 데이터를 바꾸어보자.```pythonpd.melt()df.groupby()df.pivot()```<jupyter_code>df = pd.DataFrame({'A': {0: 'a', 1: 'b', 2: 'c'},
                   'B': {0: 1, 1: 3, 2: 5},
                   'C': {0: 2, 1: 4, 2: 6}})
df
pd.melt(df, id_vars=['A'], value_vars=['B'])
df = pd.melt(df, id_vars=['A'], value_vars=['B', 'C'])
df
df.groupby("variable").sum()
df
df.pivot(columns='variable', values='value')
df = pd.DataFrame({'foo': ['one','one','one','two','two','two'],
                       'bar': ['A', 'B', 'C', 'A', 'B', 'C'],
                       'baz': [1, 2, 3, 4, 5, 6]})
df
df2 = df.pivot(index='foo', columns='bar', values='baz')
df2
df.pivot(columns='bar')['baz']
df2
df3 = df2.reset_index()
df3
df3.melt(id_vars=['foo'], value_vars=['A', 'B', 'C']).sort_values(by=['foo', 'bar'])<jupyter_output><empty_output><jupyter_text>Concat & Mergeconcat과 merge 함수를 사용하여 두 개의 DataFrame을 하나로 합칠 수 있다.<jupyter_code>s1 = pd.Series(['a', 'b'])
s1
s2 = pd.Series(['c', 'd'])
s2
pd.concat([s1, s2], ignore_index=True)
pd.concat([s1, s2], keys=['s1', 's2',], names=['Series name', 'Row ID'])
df1 = pd.DataFrame([['a', 1], ['b', 2]],
                   columns=['letter', 'number'])
df1
df2 = pd.DataFrame([['c', 3], ['d', 4]],
                   columns=['letter', 'number'])
df2
pd.concat([df1,df2])
df3 = pd.DataFrame([['c', 3, 'cat'], ['d', 4, 'dog']],
                   columns=['letter', 'number', 'animal'])
df3
pd.concat([df1, df3], sort=False)
pd.concat([df1, df3], join="inner")
adf = pd.DataFrame({"x1": ["A", "B", "C"], "x2": [1, 2, 3]})
adf
bdf = pd.DataFrame({"x1": ["A", "B", "D"], "x3": ["T", "F", "T"]})
bdf
pd.merge(adf, bdf, how='left', on='x1')
pd.merge(adf, bdf, how='right', on='x1')
pd.merge(adf, bdf, how='inner', on='x1')
pd.merge(adf, bdf, how='outer', on='x1')<jupyter_output><empty_output><jupyter_text>Groupby & Cumgroupby와 shift, cum을 활용하여 다양한 연산이 가능하다.```pythondf.groupby(col)['target'].size()df.gorupby([col1, col2])['target'].mean()df.shift(n)df.cumsum()df.cummax()df.cummin()df.cumprod()```<jupyter_code>import seaborn as sns
df = sns.load_dataset("mpg")
df.head(3)
df.groupby(by="origin")['cylinders'].size()
df["origin"].value_counts()
pd.DataFrame(df.groupby(['model_year', 'origin'])['cylinders'].mean())
df2 = pd.DataFrame(
[[4, 7, 10],
[5, 11, 8],
[6, 9, 12]],
index=[1, 2, 3],
columns=['a', 'b', 'c'])

df2
df2.shift(1)
df2['b'].shift(2)
df2.cumsum()
df2.cummax()
df2.cummin()
df2.cumprod()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.2625680435478706, 'fraction_numerical': 0.03522254242715338, 'mean_word_length': 4.659420289855072, 'pattern_counts': {'":': 4, '<': 17, '<?xml version=': 0, '>': 17, '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': '4', 'dir': 'jupyter-structured-clean-dedup', 'id': '474568', 'license': 'MIT', 'n_tokens_mistral': 1512, 'n_tokens_neox': 1491, 'n_words': 273, 'path': 'Pandas_Cheetsheet_2.ipynb', 'repo_name': 'jhbale11/Cheet_Sheet', 'size': '3123'}