How to Define a DataFrame using Pandas Library in Jupyter Notebook

Posted on

Introduction

Actually, there is a way to define a DataFrame manually in Jupyter Notebook using Pandas library. According to the information in this link , DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object. Like Series, DataFrame accepts many different kinds of input. Those input can be Dict of 1D ndarrays, lists, dicts, or Series, 2-D numpy.ndarray, Structured or record ndarray, Series and other DataFrame.

This article will show how to define DataFrame manually in Jupyter Notebook. So, run the Jupyter Notebook as follows :

(myenv) C:\python\data-science>jupyter notebook
[I 17:08:28.062 NotebookApp] Serving notebooks from local directory: C:\python\data-science
[I 17:08:28.062 NotebookApp] The Jupyter Notebook is running at:
[I 17:08:28.067 NotebookApp] http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
[I 17:08:28.068 NotebookApp]  or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
[I 17:08:28.070 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 17:08:28.225 NotebookApp]

    To access the notebook, open this file in a browser:
        file:///C:/Users/Personal/AppData/Roaming/jupyter/runtime/nbserver-8796-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b
     or http://127.0.0.1:8888/?token=4dd9801ef2aacad1d445955b0ae4621b4c669da84c617e7b


Defining DataFrame using Pandas library in Jupyter Notebook

After successfully running the Jupyter Notebook above, just execute the following script to display how to define DataFrame manually. Where there are several way to define DataFrame. The first one is defining DataFrame using a list :

import pandas as pd

list_name = ['Albert','Buck',Cole','David','Ethan']
data = pd.DataFrame(list_name)
data

The following is the example of the above command execution :

 

How to Define a DataFrame using Pandas Library in Jupyter Notebook

As in the above example, the label of the column is not exist. So, in order to define a label for the column, the following is the modification of the DataFrame definition :

import pandas as pd

list_name = ['Albert','Buck','Cole','David','Ethan']
data = pd.DataFrame({"Name":list_name})
data

The execution of the above script is in the following output :

How to Define a DataFrame using Pandas Library in Jupyter Notebook
How to Define a DataFrame using Pandas Library in Jupyter Notebook

Leave a Reply