Import a CSV file

Python Practice › Pandas Programs › Program 94

Import a CSV file

EasyPandas Programs · 94 of 95

Write a pandas program to import a CSV file.

Output
   name  age   city
0  Asha   29   Pune
1  Ravi   31  Delhi
(2, 3)

Approach

  1. pd.read_csv.

Solution

Python
import pandas as pdwith open("people.csv", "w") as f:    f.write("name,age,city\nAsha,29,Pune\nRavi,31,Delhi\n")df = pd.read_csv("people.csv")print(df)print(df.shape)
Output
   name  age   city
0  Asha   29   Pune
1  Ravi   31  Delhi
(2, 3)

Line by line

Lines 2-3

Creates a sample file so the program runs anywhere.

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Fill with the median

Python Practice › Pandas Programs › Program 93

Fill with the median

EasyPandas Programs · 93 of 95

Write a pandas program to replace NaN with the median.

Output
    name   age
0   Alex  30.0
1   John  29.0
2  Peter  29.0
3  Klaus  22.0

Approach

  1. fillna(df[col].median()).

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, np.nan, 29, 22]})df["age"] = df["age"].fillna(df["age"].median())print(df)
Output
    name   age
0   Alex  30.0
1   John  29.0
2  Peter  29.0
3  Klaus  22.0

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Fill with the most frequent value

Python Practice › Pandas Programs › Program 92

Fill with the most frequent value

EasyPandas Programs · 92 of 95

Write a pandas program to replace NaN with each column’s most frequent value.

Output
    city   age
0   Pune  30.0
1   Pune  30.0
2   Pune  29.0
3  Delhi  30.0

Approach

  1. mode().iloc[0] per column.

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"city": ["Pune", np.nan, "Pune", "Delhi"], "age": [30, np.nan, 29, 30]})print(df.fillna(df.mode().iloc[0]))
Output
    city   age
0   Pune  30.0
1   Pune  30.0
2   Pune  29.0
3  Delhi  30.0

Line by line

Line 4

df.mode().iloc[0] is the most frequent value of every column.

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Fill with the mean

Python Practice › Pandas Programs › Program 91

Fill with the mean

EasyPandas Programs · 91 of 95

Write a pandas program to replace NaN with the column mean.

Output
    name   age
0   Alex  30.0
1   John  27.0
2  Peter  29.0
3  Klaus  22.0

Approach

  1. fillna(df[col].mean()).

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, np.nan, 29, 22]})df["age"] = df["age"].fillna(df["age"].mean())print(df)
Output
    name   age
0   Alex  30.0
1   John  27.0
2  Peter  29.0
3  Klaus  22.0

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Fill from the next row

Python Practice › Pandas Programs › Program 90

Fill from the next row

EasyPandas Programs · 90 of 95

Write a pandas program to replace NaN with the value from the next row.

Output
    name   age
0   Alex  30.0
1  Peter  29.0
2  Peter  29.0
3  Klaus  22.0

Approach

  1. bfill().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, 22]})print(df.bfill())
Output
    name   age
0   Alex  30.0
1  Peter  29.0
2  Peter  29.0
3  Klaus  22.0

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Fill from the previous row

Python Practice › Pandas Programs › Program 89

Fill from the previous row

EasyPandas Programs · 89 of 95

Write a pandas program to replace NaN with the value from the previous row.

Output
    name   age
0   Alex  30.0
1   Alex  30.0
2  Peter  29.0
3  Klaus  22.0

Approach

  1. ffill().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, 22]})print(df.ffill())
Output
    name   age
0   Alex  30.0
1   Alex  30.0
2  Peter  29.0
3  Klaus  22.0

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Drop rows that are entirely missing

Python Practice › Pandas Programs › Program 88

Drop rows that are entirely missing

EasyPandas Programs · 88 of 95

Write a pandas program to drop rows where all values are missing.

Output
    name   age
0   Alex  30.0
2  Peter   NaN

Approach

  1. dropna(how="all").

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", np.nan, "Peter"], "age": [30, np.nan, np.nan]})print(df.dropna(how="all"))
Output
    name   age
0   Alex  30.0
2  Peter   NaN

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Drop columns with missing values

Python Practice › Pandas Programs › Program 87

Drop columns with missing values

EasyPandas Programs · 87 of 95

Write a pandas program to drop columns where at least one value is missing.

Output
   sr
0   1
1   2
2   3
3   4

Approach

  1. dropna(axis=1).

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"sr": [1, 2, 3, 4], "name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, 22]})print(df.dropna(axis=1))
Output
   sr
0   1
1   2
2   3
3   4

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Drop rows with any missing value

Python Practice › Pandas Programs › Program 86

Drop rows with any missing value

EasyPandas Programs · 86 of 95

Write a pandas program to drop rows where at least one value is missing.

Output
    name   age
0   Alex  30.0
2  Peter  29.0

Approach

  1. dropna().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, np.nan]})print(df.dropna())
Output
    name   age
0   Alex  30.0
2  Peter  29.0

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs

Missing values per column

Python Practice › Pandas Programs › Program 85

Missing values per column

EasyPandas Programs · 85 of 95

Write a pandas program to count the missing values in each column.

Output
name    1
age     2
dtype: int64

Approach

  1. isna().sum().

Solution

Python
import numpy as npimport pandas as pddf = pd.DataFrame({"name": ["Alex", np.nan, "Peter", "Klaus"], "age": [30, np.nan, 29, np.nan]})print(df.isna().sum())
Output
name    1
age     2
dtype: int64

Learn the concept

New to this topic? Read the lesson, then come back.

More pandas programs

← All 95 Pandas Programs