<jupyter_start><jupyter_text>Pandas Advanced Tutorial> A tutorial on pandas- toc: true - badges: true- comments: true- categories: [jupyter]- image: images/Pandas/logo.png  Pandas Tutorial<jupyter_code>import pandas as pd
import numpy as np
from numpy.random import randn
np.random.seed(42)
df = pd.DataFrame(randn(5, 4), index="A B C D E".split(), columns='W X Y Z'.split())
df<jupyter_output><empty_output><jupyter_text>Reset index<jupyter_code># Reset to default 0,1...n index
df.reset_index()
newind = 'CA NY WY OR CO'.split()
df['States'] = newind
df
df.set_index('States',inplace=True)
df<jupyter_output><empty_output><jupyter_text>Groupby<jupyter_code>import pandas as pd
# Create dataframe
data = {'Company':['GOOG','GOOG','MSFT','MSFT','FB','FB'],
       'Person':['Sam','Charlie','Amy','Vanessa','Carl','Sarah'],
       'Sales':[200,120,340,124,243,350]}
df = pd.DataFrame(data)
df
df.groupby('Company')
by_comp = df.groupby("Company")
by_comp.mean()
df.groupby('Company').mean()
by_comp.std()
by_comp.min()
by_comp.max()
by_comp.count()
by_comp.describe()
by_comp.describe().transpose()
by_comp.describe().transpose()['GOOG']<jupyter_output><empty_output><jupyter_text>apply<jupyter_code>df = pd.DataFrame({'col1':[1,2,3,4],'col2':[444,555,666,444],'col3':['abc','def','ghi','xyz']})
df.head()
def times2(x):
    return x*2
df['col1'].apply(times2)
df['col3'].apply(len)
df['col1'].sum()<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20098039215686275, 'fraction_numerical': 0.03431372549019608, 'mean_word_length': 7.766871165644171, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, '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': '647325', 'license': 'Apache-2.0', 'n_tokens_mistral': 602, 'n_tokens_neox': 567, 'n_words': 95, 'path': '_notebooks/2021-05-01-Pandas Advanced.ipynb', 'repo_name': 'smsrikanthreddy/blog', 'size': '1428'}