Filtering using mask and where in pandas

  03 Apr 2019
   python

Filtering a dataframe can be achieved in multiple ways using pandas. There are times when you simply need to update a column based on a condition which is true or vice-versa. In pandas dataframe there are some inbuilt methods to achieve the same using .where() and .mask().

  • df.where - Replace value when condition is false
  • df.mask - Replace value when condition is true

Initiating a dummy dataframe with some columns to understand the same -

import pandas as pd
import numpy as np
dummy_data = pd.DataFrame(
    np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
    columns=['Column 1', 'Column 2', 'Column 3'])
dummy_data
Column 1Column 2Column 3
01.02.03.0
14.05.0NaN
27.08.09.0
33.02.0NaN
45.06.0NaN

  String Interpolation in Python

  28 Mar 2019
   python

String operations in python, sometimes can be a bit tedious, specially when we need to pass variables within Strings.

Although there are multiple ways to achieve the same, but some of the them are

  • String interpolation
  • str.format()

Declaring some variables -

Name = 'Python'
Framework = 'Django'
  • Using str.format()

"Language - {0}, Framework - {1}".format(Name, Framework)

‘Language - Python, Framework - Django’

  • Using f-strings format - string interpolation

f"Language - {Name}, Framework - {Framework}"

‘Language - Python, Framework - Django’


  Join, Merge, Append and Concatenate

  25 Mar 2019
   python

Working with multiple data frames often involves joining two or more tables to in bring out more no. of columns from another table by joining on some sort of relationship which exists within a table or appending two tables which is adding one or more table over another table with keeping the same order of columns.


Example of append data -> monthly files of revenue sheets of a company and wee need at end of the year to be clubbed into single table.

alt text      alt text

alt text

alt text