Attributes, Methods and Functions in python

  07 Jan 2019
   python

Understand the concept of attributes, methods and functions under the context of a dataframe

Attributes

Attributes are the features of any object. They can be accessed by following a dot and the name of the following attribute.

  • For example: person.age, person.height
    here, age and height are the attributes of the person object

Methods and Functions

Methods are always associated with an object where as the Functions are not dependent on any object. In simple term a method is on a object where as a function is independent of object.

  • For example: math.ceil(), dataframe.describe() are methods whereas sum(), len() are python built in functions

  Subsetting a dataframe in pandas

  05 Jan 2019
   python

Importing packages and datasets

import pandas as pd
# Fetching data from url as csv by mentioning values of various paramters
data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
                   header = None,
                   index_col = False,
                   names = ['sepal_length','sepal_width','petal_length','petal_width','iris_class'])
# Unique classes of iris datasets
data.iris_class.unique()

array([‘Iris-setosa’, ‘Iris-versicolor’, ‘Iris-virginica’], dtype=object)

Subsetting

data_setosa = data[data.iris_class == 'Iris-setosa']
data_versicolor = data[data.iris_class == 'Iris-versicolor']
data_virginica = data[data.iris_class == 'Iris-virginica']

'''
Now we can have a look at descriptive statistics summary for each of the subset and can make inference like following -
* Each of the subset is of same size i.e., 50
* Average Sepal and Petal Length is lowest in setosa and highest in virginica
'''

  Read data into pandas

  23 Dec 2018
   python

Provide column names while reading a dataset in pandas

# Import the required modules
import pandas as pd

Reading the dataset using read.csv() function with mentioning column names in names parameters.

#Fetching data from url as csv by mentioning values of various paramters
data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/iris/iris.data",
                   header = None,
                   index_col = False,
                   names = ['sepal_length','sepal_width','petal_length','petal_width','iris_class'])