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

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