DEV Community

Cover image for Powerful Pandas! Part 2
Aziza Afrin
Aziza Afrin

Posted on

Powerful Pandas! Part 2

This is the second blog on Pandas Library. In this write up we would show what else can be done with Pandas library apart from content mentioned in Powerful Pandas! Part 1.

Dealing with Missing Values

While analyzing data we often face a problem of missing values in our data. Let's see how can we deal with that in python with pandas library.
Let's import pandas and numpy library into our workspace.

import numpy as np
import pandas as pd
Enter fullscreen mode Exit fullscreen mode

Now create a data-frame with some missing value.

d={'A':[1,2,np.nan],'B':[5,np.nan,np.nan],'C':[1,2,3]}
Enter fullscreen mode Exit fullscreen mode
df=pd.DataFrame(d)
df
Enter fullscreen mode Exit fullscreen mode

1. Dropping Missing Values:

To drop all the rows with missing values:

df.dropna()
Enter fullscreen mode Exit fullscreen mode

To drop all the column with missing values:

df.dropna(axis=1)
Enter fullscreen mode Exit fullscreen mode

We can also set limit like how many missing entries are allowed.

#it keeps row 1, as it has at lest 2 not NaN entry
df.dropna(thresh=2)
Enter fullscreen mode Exit fullscreen mode

2. Replacing Missing Values:

we can replacing missing entries with anything like string, average values etc.

#replacing missing values
df['A'].fillna(value=df['A'].mean())
Enter fullscreen mode Exit fullscreen mode

Joining Rows

Using group by method to light a group rows of data together and call aggregate functions.
Groupby allows you to group together rows based off of a column and perform an aggregate function on them

data={'Company':['Gp','Gp','Teletalk','Teletal','Robi','Robi'],
                'Person':['Alam','Mim','Tina','Rimi','Rafi','Pranto'],
                'Sales':[200,170,225,439,410,324]}
Enter fullscreen mode Exit fullscreen mode
df=pd.DataFrame(data)
Enter fullscreen mode Exit fullscreen mode
df_new=df.groupby('Company')
Enter fullscreen mode Exit fullscreen mode
df_new.mean()
Enter fullscreen mode Exit fullscreen mode

Check out this link for practice and comment down your thoughts.(https://www.kaggle.com/code/azizaafrin/powerful-pandas-part-2)
Happy Learning!❤️

Aziza Afrin

Top comments (0)