<jupyter_start><jupyter_text>PandasPandas is an open source library which provides high-performance, easy-to-use data structures and data analysis tools for the Python programming language. Pandas has a lot of functions that will help in reading and writing data and also for data manipulation. Thus we will be using pandas throughout the course.Pandas DataframesDataFrame is an object for data manipulation. You can think of it as a 2D tabular structure, where every row is a dataset entry and columns represents features of data.Creating copy of DataFramedf = iris Above statement simply makes df refer to the data frame object that iris is referring to. So now both iris and df refer to the same dataframe object and any changes done via one will reflect in other.So effectively this is not creating another dataframe object. And if we wish to create a copy then we will use copy() function for that purpose:df = iris.copy()<jupyter_code># first we need to import pandas as pd
import pandas as pd
# here we will use the iris csv dataset, you can get this dataset at the link below
# csv:each row is separated nad column is separated by commas
iris = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
print(iris)
print(type(iris))
# here we are making a copy of the dataset so that we can make as many changes as we want and can have different changes in different 
# files as well
df = iris.copy()
# return first 5 rows by default
df.head()
# return first 4 rows
df.head(4)
# return last 3 rows
df.tail(3)
# shape tells us aboyt the shape of the dataset(149 rows and 5 columns in this case)
print(df.shape)
# gives the information about the datatype
print(df.dtypes)
print(df.columns)
print()
# we can change the names of the column as well
df.columns = ['sl','sw','pl','pw','flower']
print(df.columns)
# describes give syou the statistical info about the data
print(df.describe())
# return a complete column
df["sl"]
df.flower
df["flower"]
# return the number of null entries in each column
df.isnull().sum()
# almost works like slicing
df.iloc[1:4,2:3]
# no change because we already have a copy as df,so no change is reflected 

df.drop(0)
# no change because we already have a copy as df,so no change is reflected 
df.head()
# But here you can notice that first row is dropped because now we hhave saved df.drop(0) in "a",so changes are reflected in 
# the output now
a = df.drop(0)
a.head()
# Drop by label
# if we want change in the df itself than 'inplace = True' is used
df.drop(1 , inplace = True)
df.head()
# here 2 3 4 5 6 are labels
df.columns
df.index
# in the output we can see that index are still still same strating from 0,so we made changes in labels, not index
# so index 0 corresponds to label 2 and index 5 corresponds to label 7
df.index[0],df.index[5]
# we can also drop a row using index instead of labels
df.drop(df.index[0])

df.head()
# we use inplace= True to make changes in df itself
df.drop(df.index[0],inplace = True)
df.head()
# rows at index 0 and 1 are dropped
df.drop(df.index[[0,1]],inplace = True)
df.head()

df.drop(df.index[[0,1]],inplace = True)
df.head()
df.drop(df.index[0],inplace = True)
df.head()
# if the value in "sl" column is less than 5 than return True else false
df.sl<5
# return only the rows with values greater than 5
df[df.sl>5]
# return only the rows with values less than 3 in "pw" column
df[df.pw<3]
# describe only for 'Iris-setosa' in flower column
df[df.flower == 'Iris-setosa']
df.describe()
print(df.head())
print(df.iloc[0:3,4])
# iloc takes the index as argument
print(df.iloc[8])
# loc takes the label as argument
df.loc[8]
df.index
# we an reset the index as well
df.reset_index()
df.index
# to remove old column of labels and get a fresh column of index
df.reset_index(drop = True)
# to make changes in df itself
df.reset_index(drop = True,inplace = True)
df.index
# deleting a column
df.drop('pw',axis = 1,inplace = True)
df.head()
# we can make a new copy of iris so as to get all the data back
df = iris.copy()
df.describe()
df.columns = ['sl','sw','pl','pw','flower']
df.columns
# Same as the initial data we started with
df.head()
# adding a column "diff_pl-pw" that gives the difference of "pl" and "pw"
df["diff_pl_pw"] = df["pl"]-df["pw"]
df.tail()
df["diff of sl and sw"] = df["sl"] - df["sw"]
df.tail()
# adding a new column with value as '1'
df["abc"] = 1
df.head()
df["pl"] - df["pw"]
df.head()
# giving '0' as the value to the positions given in iloc
df.iloc[2:4 , 1:3] = 0
df.head()
import numpy as np
# giving 'np.nan' as the value to the positions given in iloc
df.iloc[2:4 , 1:3] = np.nan
df.head()
df.describe()
# drops the row with 2 and 3 labels because they have Nan values
df.dropna()
df.head()
# to make changes in df itself
df.dropna(inplace = True)
df.head()
df.iloc[2:4,1:3] = np.nan
df.head()
df.dropna()
df.drop(145)
# if there is Nan in the dataset and we want to fill it with the mean of the values in "sw" column
# since we have not used inplace,changes are not reflected in the output
df.sw.fillna(df.sw.mean())
df.head()
# changes are rlected when we use inplace= True
df.sw.fillna((df.sw.mean()),inplace = True)
df.head()
# same we will do with "pl" column
df.pl.fillna((df.pl.mean()),inplace = True)
df.head()
# adding a new column as "gender"
df["Gender"] = "Female"
df.head()
# Replacing values with Male
df.iloc[0:10 , 6] = "Male"
df.head(11)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10736026563364692, 'fraction_numerical': 0.014388489208633094, 'mean_word_length': 4.336614173228346, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 6, 'https://': 1, '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': '427693', 'license': 'MIT', 'n_tokens_mistral': 1813, 'n_tokens_neox': 1750, 'n_words': 791, 'path': 'algorithms/ml/Pandas/Pandas.ipynb', 'repo_name': 'Nikki942/algorithms', 'size': '5421'}