<jupyter_start><jupyter_text>Pandas - Operações* Tópicos    * Unique Values    * Select Slices    * Apply Functions    * Sort Values<jupyter_code># Import Libraries

import numpy as np
import pandas as pd

# Init Dataframe

dic1 = {'col1':[1,2,3,4],
        'col2':[444,555,666,444],
        'col3':['abc','def','ghi','xyz']}

df = pd.DataFrame(dic1)

df.head()
# Unique values

print(df['col2'].unique())
print()

print(np.unique(df['col2']))
print()

# Number of unique values

print(df['col2'].nunique())
print()

# Count number of times a unique value appears

print(df['col2'].value_counts())
print()

# Select slices of Dataframe

df1 = df[(df['col1']>2) & (df['col2']==444)]
print(df1)
print()
# Apply Functions: defined

def times2(x):
    return x*2

print(df['col1'].apply(times2))
print()

print(df['col3'].apply(len))
print()

print(df['col1'].sum())
print()

# Apply functions 2: lambda

print(df['col1'].apply(lambda x: x*x))
print()
# Sorting Values

print(df)
print()

print(df.sort_values(by='col2'))
print()

# Pivot Table: Create Multilevel (same as dynamic table of Excel)

dic1 = {'A':['foo','foo','foo','bar','bar','bar'],
        'B':['one','one','two','two','one','one'],
        'C':['x','y','x','y','x','y'],
        'D':[1,3,2,5,4,1]}

df1 = pd.DataFrame(dic1)
print(df1)
print()

print(df1.pivot_table(values='D',index=['A', 'B'],columns=['C']))
print()<jupyter_output>col1  col2 col3
0     1   444  abc
1     2   555  def
2     3   666  ghi
3     4   444  xyz

   col1  col2 col3
0     1   444  abc
3     4   444  xyz
1     2   555  def
2     3   666  ghi

     A    B  C  D
0  foo  one  x  1
1  foo  one  y  3
2  foo  two  x  2
3  bar  two  y  5
4  bar  one  x  4
5  bar  one  y  1

C          x    y
A   B            
bar one  4.0  1.0
    two  NaN  5.0
foo one  1.0  3.0
    two  2.0  NaN
{'fraction_non_alphanumeric': 0.17857142857142858, 'fraction_numerical': 0.06758241758241758, 'mean_word_length': 2.950108459869848, 'pattern_counts': {'":': 0, '<': 4, '<?xml version=': 0, '>': 5, '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': '1', 'dir': 'jupyter-structured-clean-dedup', 'id': '569838', 'license': 'MIT', 'n_tokens_mistral': 805, 'n_tokens_neox': 757, 'n_words': 200, 'path': 'scripts_numpy_pandas/Pandas_05_Operations.ipynb', 'repo_name': 'davidcoelho89/MLTool_python', 'size': '1820'}