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
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]}
df=pd.DataFrame(d)
df
1. Dropping Missing Values:
To drop all the rows with missing values:
df.dropna()
To drop all the column with missing values:
df.dropna(axis=1)
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)
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())
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]}
df=pd.DataFrame(data)
df_new=df.groupby('Company')
df_new.mean()
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)