Row with the minimum value

Python Practice › Pandas Programs › Program 59

Row with the minimum value

EasyPandas Programs · 59 of 95

Write a pandas program to find the row where a column is smallest.

Output
name       Alex
salary    50000
Name: 0, dtype: object

Approach

  1. idxmin().

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "salary": [50000, 65000, 63000, 59000]})print(df.loc[df["salary"].idxmin()])
Output
name       Alex
salary    50000
Name: 0, dtype: object

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Row with the maximum value

Python Practice › Pandas Programs › Program 58

Row with the maximum value

EasyPandas Programs · 58 of 95

Write a pandas program to find the row where a column is largest.

Output
name       John
salary    65000
Name: 1, dtype: object

Approach

  1. df.loc[df["salary"].idxmax()].

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "salary": [50000, 65000, 63000, 59000]})print(df.loc[df["salary"].idxmax()])
Output
name       John
salary    65000
Name: 1, dtype: object

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Column values as a list

Python Practice › Pandas Programs › Program 57

Column values as a list

EasyPandas Programs · 57 of 95

Write a pandas program to get the values of a column as a list.

Output
['Alex', 'John', 'Peter']

Approach

  1. df["col"].tolist().

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter"], "salary": [50000, 65000, 63000]})print(df["name"].tolist())
Output
['Alex', 'John', 'Peter']

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Rename one column

Python Practice › Pandas Programs › Program 56

Rename one column

EasyPandas Programs · 56 of 95

Write a pandas program to rename one column.

Output
   Sr.no  name
0      1  Alex
1      2  John

Approach

  1. rename(columns={"old": "new"}).

Solution

Python
import pandas as pddf = pd.DataFrame({"Rank": [1, 2], "name": ["Alex", "John"]})print(df.rename(columns={"Rank": "Sr.no"}))
Output
   Sr.no  name
0      1  Alex
1      2  John

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Shuffle rows

Python Practice › Pandas Programs › Program 55

Shuffle rows

EasyPandas Programs · 55 of 95

Write a pandas program to shuffle rows.

Output
    name  age
3  Klaus   33
1   John   27
0   Alex   30
2  Peter   29

Approach

  1. sample(frac=1); seed with random_state.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})print(df.sample(frac=1, random_state=3))
Output
    name  age
3  Klaus   33
1   John   27
0   Alex   30
2  Peter   29

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Count NaN values

Python Practice › Pandas Programs › Program 54

Count NaN values

EasyPandas Programs · 54 of 95

Write a pandas program to count NaN values.

Output
2

Approach

  1. isna().sum().sum().

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Replace NaN with a value

Python Practice › Pandas Programs › Program 53

Replace NaN with a value

EasyPandas Programs · 53 of 95

Write a pandas program to replace NaN in a column with a fixed value.

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

Approach

  1. fillna(value).

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Population per country

Python Practice › Pandas Programs › Program 52

Population per country

EasyPandas Programs · 52 of 95

Write a pandas program to total the population per country from a data set.

Output
country
China    46700000
India    40300000
USA        980000
Name: population, dtype: int64

Approach

  1. groupby("country")["population"].sum().

Solution

Python
import pandas as pddata = pd.DataFrame({    "country": ["India", "China", "India", "USA", "China"],    "city": ["Pune", "Shanghai", "Delhi", "Austin", "Beijing"],    "population": [7_400_000, 24_900_000, 32_900_000, 980_000, 21_800_000],})print(data.groupby("country")["population"].sum().sort_values(ascending=False))
Output
country
China    46700000
India    40300000
USA        980000
Name: population, dtype: int64

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Write a tab-separated file

Python Practice › Pandas Programs › Program 51

Write a tab-separated file

EasyPandas Programs · 51 of 95

Write a pandas program to write a DataFrame to a CSV file with a tab separator.

Output
name	age	salary
Alex	30	50000
John	27	65000

Approach

  1. to_csv(sep="\t").

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "age": [30, 27], "salary": [50000, 65000]})df.to_csv("people.tsv", sep="\t", index=False)print(open("people.tsv").read())
Output
name	age	salary
Alex	30	50000
John	27	65000

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reorder columns

Python Practice › Pandas Programs › Program 50

Reorder columns

EasyPandas Programs · 50 of 95

Write a pandas program to change the order of columns.

Output
   salary  name  age
0   50000  Alex   30
1   65000  John   27

Approach

  1. Select columns in the new order.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "age": [30, 27], "salary": [50000, 65000]})print(df[["salary", "name", "age"]])
Output
   salary  name  age
0   50000  Alex   30
1   65000  John   27

Learn the concept

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

More pandas programs

← All 95 Pandas Programs