Pandas Basics — 2

Devil’s Advocate
2 min readFeb 23, 2023

--

We will look at a few more Pandas functions in this post.

info(): returns a summary of information about a DataFrame, including the size & data types of each column and the number of non-null values.

The info() and the describe() functions seem similar. But they are different because the info() function returns the information about the columns and the describe() function generates a statistical summary for the numerical columns of a data frame.

For example:

import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie'],
'Age': [25, 30, 35],
'Salary': [50000, 70000, 90000]}
df = pd.DataFrame(data)
df.info()
df.describe()

loc[]: allows you to access a subset of rows and columns of a DataFrame by label-based indexing. For example:

import pandas as pd
df = pd.read_csv('data.csv')
df.loc[10:20, ['col1', 'col2']]

The loc() function can also be used to filter rows based on a condition. Let’s load a data frame afresh and show it.

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.loc[df['A'] > 1]
# A B
#1 2 5
#2 3 6

iloc[]: allows you to access a subset of rows and columns of a DataFrame by integer-based indexing. It is similar to loc() but works with positional indexing. For example:

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df.iloc[0:2, 0:2]

drop(): removes one or more columns or rows from a DataFrame. For example:

import pandas as pd
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': [7, 8, 9]})
df.drop('A', axis=1)

--

--

Devil’s Advocate

Seeker for life. Looking to make technology simpler for everyone.