pandas.DataFrame/pandas.core.frame.DataFrame - The Table Class

This section describes pandas.DataFrame/pandas.core.frame.DataFrame class, which represents a table of data with rows and columns.

What Is pandas.DataFrame/pandas.core.frame.DataFrame? pandas.DataFrame/pandas.core.frame.DataFrame is a class that represents a tabular structure with potentially heterogeneously-typed columns.

Main features of pandas.DataFrame are:

Here are some basic properties, operations and methods provided in pandas.DataFrame class.

1. pd.DataFrame() - Method to construct a new DataFrame object. The example below uses a JSON string to provide input.

>>> import pandas as pd

df = pd.DataFrame({
  "Name": [
    "Braund, Mr. Owen Harris",
    "Allen, Mr. William Henry",
    "Bonnell, Miss. Elizabeth",
  "Age": [22, 35, 58],
  "Sex": ["male", "male", "female"],

>>> type(df)
<class 'pandas.core.frame.DataFrame'>

>>> print(df)
                       Name  Age     Sex
0   Braund, Mr. Owen Harris   22    male
1  Allen, Mr. William Henry   35    male
2  Bonnell, Miss. Elizabeth   58  female

2. df[col_name] or df.col_name - Operation to return a Series object representing data elements of the given column.

>>> age = df["Age"]
>>> age = df.Age

>>> type(age)

<class 'pandas.core.series.Series'>

>>> print(age)
0    22
1    35
2    58
Name: Age, dtype: int64

3. df[col_name][index], df.col_name[index] or[index, col_name] - Operation to return the data element of the given index and the given column.

>>> a2 = df["Age"][2]
>>> a2 = df.Age[2]
>>> a2 =[2, "Age"]

>>> type(a2)
<class 'numpy.int64'>

>>> print(a2)

4. df.iat[i, j] - Operation to return the data element of the given row position and the given column position of the DataFrame.

>>> a2 = df.iat[2, 1]

>>> type(a2)
<class 'numpy.int64'>

>>> print(a2)

5. df.columns - Property holding an Index object representing the column list.

>>> cols = df.columns

>>> type(cols)
<class 'pandas.core.indexes.base.Index'>

>>> print(cols)
Index(['Name', 'Age', 'Sex'], dtype='object')

6. df.shape - Property holding shape (number of rows and number of columns) of of the DataFrame.

>>> print(df.shape)
(3, 3)

7. df.append() - Method to append a Dict object as a row to the DataFrame.

>>> df.append({"Name": "John Smith", "Age": 18, "Sex": "male"})

8. df.groupby(column_list) - Method to return pandas.core.groupby.generic.DataFrameGroupBy object, which supports count() and other methods.

>>> grp = df.groupby(['Sex']).count()
        Name  Age
female  1     1
male    2     2

Table of Contents

 About This Book

 Running Python Code Online

 Python on macOS Computers

 Python on Linux Computers

 Built-in Data Types

 Variables, Operations and Expressions

 Statements - Execution Units

 Function Statement and Function Call

 Iterators, Generators and List Comprehensions

 Classes and Instances

 Modules and Module Files

 Packages and Package Directories

 "sys" and "os" Modules

 "pathlib" - Object-Oriented Filesystem Paths

 "pip" - Package Installer for Python - Python Libraries for Science

pandas - Data Analysis and Manipulation

 What Is 'pandas'

pandas.DataFrame/pandas.core.frame.DataFrame - The Table Class

 pandas.core.series.Series - The Column Class

 File Input and Output for DataFrame

 Anaconda - Python Environment Manager

 Jupyter Notebook and JupyterLab


 Full Version in PDF/EPUB