<jupyter_start><jupyter_text>PANDAS - Panel Datas<jupyter_code># To install pandas if not installed in the system.
!pip install pandas
import pandas as pd
import numpy as np
# Series ha just 1 column.
s1 = pd.Series([1,2,3,4,5])
s1
type(s1)
s2 = pd.Series([6,7,8,9,np.nan])
s2
type(s2)
# Datframe has multiple columns.
s3 = pd.DataFrame([10,20,30,40,50])
s3
s31 = pd.DataFrame([[10,20,30,40,50], [100,200,300,400,500]])
s31
type(s3)
s32 = pd.DataFrame([[10,20,30,40,50], [100,200,300,400,500], [1000,2000,3000,4000,5000]])
s32
s4 = pd.DataFrame([[10,20,30,40,50], [100,200,300,400,500], [1000,2000,3000,4000,5000]], 
                  index = ["ROW1", "ROW2", "ROW3"] )
s4
s41 = pd.DataFrame(data = [[10,20,30,40,50], [100,200,300,400,500], [1000,2000,3000,4000,5000]], 
                  index = ["ROW1", "ROW2", "ROW3"],
                  columns=["A", "B", "C", "D", "E" ])
s41
s42 = pd.DataFrame(data = [[10,20,30,40,50], [100,200,300,400,500], [1000,2000,3000,4000,5000]], 
                  index = ["ROW1", "ROW2", "ROW3"],
                  columns=["A", "B", "C", "D", "E" ],
                  dtype=float)
s42
from numpy.random import rand as rn
np.random.seed(101)
s5 = rn(5,4)
s5
row_labels = ["A", "B", "C", "D", "E"]
column_labels = ["P1", "P2", "P3", "P4"]

pd.DataFrame(data = s5, index = row_labels, columns = column_labels)
s6 = np.array([ [1,2,3],
                [10,20,30],
                [100,200,300] ])
type(s6)
s6
s7 = pd.DataFrame(s6, ["A", "B", "C"], ["ONE","TWO","THREE"])
s7
type(s7)
# Viewing the data

s7.head(1)
s7.tail(1)
s7.index
s7.columns
## Important difference between numpy array and pd dataframe:
# Arrays has only one data type thoughout the entire dataset.
# Dataframe has only one data type throughout one column.
s7.describe()
s7.T
s7.sort_index(axis = 0, ascending= False)
s7.sort_index(axis = 1, ascending= False)
# Selecting

s7.loc["B"]
s7
s7[0:2] # Only Rows to be selected from row0-row2
s7[2:3]
## loc vs. iloc(Index must be selected , then columns can be selected)

#loc
s7.loc["A"]
s7.loc["A":"C"]
s7.loc["A":"C", "THREE"]
# iloc

s7.iloc[0]
s7.iloc[0:3]
s7.iloc[0:3, 2]
# Data imputation

s8 = s7.reindex(columns = list(s7.columns)+["E"])
s8
s8.iloc[0:2, 3] = 13
s8
s8.dropna(how = "any") # This removes the entire index containing the NA values.
s8.fillna(value = 1331)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.20127388535031848, 'fraction_numerical': 0.14819532908704883, 'mean_word_length': 3.970464135021097, 'pattern_counts': {'":': 2, '<': 5, '<?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': '594827', 'license': 'Apache-2.0', 'n_tokens_mistral': 1192, 'n_tokens_neox': 1003, 'n_words': 235, 'path': 'Python Intermediary_Self Course_Module 2.1_PANDAS.ipynb', 'repo_name': 'avishekdeb13/Python-Basics-Tutorial', 'size': '2355'}