# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3 (ipykernel)
#     language: python
#     name: python3
# ---

import pandas as pd
import numpy as np

iris =  pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
iris.head()

df = iris.copy()
df.head()

df.dtypes

df.columns

df.columns = ['sl','sw','pl','pw','flower_types']
df.head()

df.shape

df.describe()

df.pw

df['sw']

df.iloc[1:6,2:4]

df.loc[1:5]

df.isnull()

df.isnull().sum()

# +
#To get the indexs of the dataframe, index attribute is used

iris.index
# -

df.head()

# drop() function is used to delete a row from the dataset
df.drop(0)
df

# +
#No changes have been made because drop function does not delete the rows from the original dataframe. 
#It creates a copy of the dataframe and returns the updated copy

#To make changes in the original dataframe we need to pass a inplace=True argument. This ensures that the changes are made 
#in the original dataset

iris.drop(0, inplace=True)
iris.head()

# +
#To delete more than one columns in one go, we can pass a list of row numbers

iris.drop([4,5,8], inplace=True)
iris.head(10)

# +
# index[i] is used to access the ith row and not the row whose row no is i

print(iris.index[0])


#Therefore iris.index[i] can also be passed to drop function to delete the ith row
iris.drop(iris.index[0], inplace=True)
iris.head(10)

# +
#to get the ith row from starting we use iloc

print(iris.head())
iris.iloc[0]

# +
#Similarly to access the ith label row, we use loc

print(iris.head())
iris.loc[2]

# +
#To add a row into a dataframe, 

iris.loc[0] = [1, 2, 3, 4, "Iris-setosa"]

#The entry will be added to the end
iris.tail()

# +
#To reset the indices, reset_index() function is used

print("Dataframe before reset index")
print(iris.head(10))
iris.reset_index(inplace=True)
print("\nDataframe after reset index")
print(iris.head(10))

#Note that in the above dataframe the indices before resetting index are added as a column in our updated dataframe
# -

#removing column
df.drop('pl', axis= 1, inplace=True)
df.head()

#another way to delete a column is by using del
del df['sl']
df.head()

df.head()

df =  pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data")
df.head()

df.columns = ['sl','sw','pl','pw','flower_types']
df.head()

#adding new column
df['add_sl_sw'] = df['sl'] + df['sw']
df.head()

df.iloc[2:4, 1:3] = np.nan
df.head()

df.dropna(inplace=True)
df.head()

df.iloc[2:4, 1:3] = np.nan
df.head()

setosa = df[df['flower_types']=='Iris-setosa']
setosa_sw_mean = setosa['sw'].mean()
print(setosa_sw_mean)

setosa_pl_mean = setosa['pl'].mean()
print(setosa_pl_mean)

df['sw'].fillna(setosa_sw_mean, inplace=True)
df['pl'].fillna(setosa_pl_mean, inplace=True)


df.head()

df['gender'] = 'Female'
df.head()

df.iloc[1:20,6] = 'Male'
df.head()


# +
def f(s):
    if s == 'Male':
        return 0
    else:
        return 1

df['sex'] = df['gender'].apply(f)
df.head()
# -

del df['gender']
df.head()

# ## Problem Statement
# ####  FInd and print count of each kind of flower ( separated by space) ?

df.count()

df.flower_types.value_counts()

# #### Find the data of flower "Iris-virginica" type where petal-length > 1.5 ?

iris_virginica = df[df['flower_types'] == 'Iris-virginica'] 

iris_virginica[iris_virginica.pl > 1.5]

# #### Find and print the maximum, minimum and average values of the feature for each kind of flower ?

df.describe().loc[['min','max','mean']]



StarcoderdataJupyterScriptsDedupFiltered