Select text columns

Python Practice › Pandas Programs › Program 69

Select text columns

EasyPandas Programs · 69 of 95

Write a pandas program to select the text columns.

Output
   name   city
0  Alex   Pune
1  John  Delhi

Approach

  1. select_dtypes(include=...).

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "city": ["Pune", "Delhi"], "age": [30, 27]})print(df.select_dtypes(exclude="number"))
Output
   name   city
0  Alex   Pune
1  John  Delhi

Line by line

Line 3

Excluding numeric columns works on both old (object) and new (str) text types.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reverse column order

Python Practice › Pandas Programs › Program 68

Reverse column order

EasyPandas Programs · 68 of 95

Write a pandas program to reverse the order of columns.

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

Approach

  1. df[df.columns[::-1]].

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Reverse row order

Python Practice › Pandas Programs › Program 67

Reverse row order

EasyPandas Programs · 67 of 95

Write a pandas program to reverse the order of rows.

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

Approach

  1. iloc[::-1].

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Remove the last n rows

Python Practice › Pandas Programs › Program 66

Remove the last n rows

EasyPandas Programs · 66 of 95

Write a pandas program to remove the last n rows.

Output
   name  age
0  Alex   30
1  John   27

Approach

  1. iloc[:-n].

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Remove the first n rows

Python Practice › Pandas Programs › Program 65

Remove the first n rows

EasyPandas Programs · 65 of 95

Write a pandas program to remove the first n rows.

Output
    name  age
2  Peter   29
3  Klaus   33

Approach

  1. iloc[n:].

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

All columns except one

Python Practice › Pandas Programs › Program 64

All columns except one

EasyPandas Programs · 64 of 95

Write a pandas program to select all columns except one.

Output
   name  salary
0  Alex   50000
1  John   65000

Approach

  1. drop(columns=...).

Solution

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

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Index of a column

Python Practice › Pandas Programs › Program 63

Index of a column

EasyPandas Programs · 63 of 95

Write a pandas program to find the position of a column.

Output
2

Approach

  1. df.columns.get_loc("col").

Solution

Python
import pandas as pddf = pd.DataFrame({"sr_no": [1], "name": ["Alex"], "age": [30], "salary": [50000]})print(df.columns.get_loc("age"))
Output
2

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

List of lists to DataFrame

Python Practice › Pandas Programs › Program 62

List of lists to DataFrame

EasyPandas Programs · 62 of 95

Write a pandas program to convert a list of lists into a DataFrame.

Input
[["Virat", "cricket"], ["Messi", "football"]]
Output
    name     sport
0  Virat   cricket
1  Messi  football

Approach

  1. Pass columns=.

Solution

Python
import pandas as pdprint(pd.DataFrame([["Virat", "cricket"], ["Messi", "football"]], columns=["name", "sport"]))
Output
    name     sport
0  Virat   cricket
1  Messi  football

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Column data types

Python Practice › Pandas Programs › Program 61

Column data types

EasyPandas Programs · 61 of 95

Write a pandas program to get the data type of each column.

Output
name          str
age         int64
salary    float64
dtype: object

Approach

  1. dtypes.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John"], "age": [30, 27], "salary": [50000.0, 65000.5]})print(df.dtypes)
Output
name          str
age         int64
salary    float64
dtype: object
Note

pandas 3 shows text columns as str; older versions show object.

Learn the concept

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

More pandas programs

← All 95 Pandas Programs

Is a column present?

Python Practice › Pandas Programs › Program 60

Is a column present?

EasyPandas Programs · 60 of 95

Write a pandas program to check whether a column exists.

Output
True False

Approach

  1. "col" in df.columns.

Solution

Python
import pandas as pddf = pd.DataFrame({"name": ["Alex"], "salary": [50000]})print("salary" in df.columns, "bonus" in df.columns)
Output
True False

Learn the concept

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

More pandas programs

← All 95 Pandas Programs