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

Let see some examples of atrributes, methods and functions in context of pandas dataframe :

# Load the pandas package, import data and pass column names in names parameter
import pandas as pd
data = pd.read_csv("https://archive.ics.uci.edu/ml/machine-learning-databases/auto-mpg/auto-mpg.data",
                   header = None,
                   delim_whitespace = True,
                   names = ['mpg','cylinders','displacement','horsepower','weight',
                            'acceleration','model year','origin','car name'])

Some of the Atrributes associated with this dataframe

  • .dtypes

data.dtypes
mpg             float64
cylinders         int64
displacement    float64
horsepower       object
weight          float64
acceleration    float64
model year        int64
origin            int64
car name         object
dtype: object

  • .columns

data.columns

Index([‘mpg’, ‘cylinders’, ‘displacement’, ‘horsepower’, ‘weight’, ‘acceleration’, ‘model year’, ‘origin’, ‘car name’], dtype=’object’)


  • .shape

data.shape

(398, 9)


Some of the methods associated :

  • describe()

data.describe()
mpgcylindersdisplacementweightaccelerationmodel yearorigin
count398.000000398.000000398.000000398.000000398.000000398.000000398.000000
mean23.5145735.454774193.4258792970.42462315.56809076.0100501.572864
std7.8159841.701004104.269838846.8417742.7576893.6976270.802055
min9.0000003.00000068.0000001613.0000008.00000070.0000001.000000
25%17.5000004.000000104.2500002223.75000013.82500073.0000001.000000
50%23.0000004.000000148.5000002803.50000015.50000076.0000001.000000
75%29.0000008.000000262.0000003608.00000017.17500079.0000002.000000
max46.6000008.000000455.0000005140.00000024.80000082.0000003.000000

  • head() and tail()

data.head()
mpgcylindersdisplacementhorsepowerweightaccelerationmodel yearorigincar name
018.08307.0130.03504.012.0701chevrolet chevelle malibu
115.08350.0165.03693.011.5701buick skylark 320
218.08318.0150.03436.011.0701plymouth satellite
316.08304.0150.03433.012.0701amc rebel sst
417.08302.0140.03449.010.5701ford torino

data.tail()
mpgcylindersdisplacementhorsepowerweightaccelerationmodel yearorigincar name
39327.04140.086.002790.015.6821ford mustang gl
39444.0497.052.002130.024.6822vw pickup
39532.04135.084.002295.011.6821dodge rampage
39628.04120.079.002625.018.6821ford ranger
39731.04119.082.002720.019.4821chevy s-10

Some of the functions which can be applied :

  • len()

len(data)

398


  • range(), list() and type()

x = range(6)
list(x)

[0, 1, 2, 3, 4, 5]

type(x)

range


Applying a combination of attribute, method and function on the pandas dataframe

    • .loc and .aggregate() with sum() on data object

data.loc[:,'mpg': 'displacement'].aggregate(sum)
mpg              9358.8
cylinders        2171.0
displacement    76983.5
dtype: float64