How To Create Pandas Object?

Table Of Contents:

  1. What Is An Pandas Object?
  2. How To Create Pandas Data Frame?
  3. Creating Pandas Object Using Dictionary.
  4. Creating Pandas Object Using CSV File.
  5. Creating Pandas Object Using Excel File.
  6. Creating Pandas Object Using SQL Table.

(1) What Is An Pandas Object?

  • A data table with more than one row and column is considered a Pandas object.
  • For example, a student table having the student’s details like name, roll number, mark etc. is considered a pandas object.
  • This panda’s objects are called Pandas DataFrame.

(2) How To Create Pandas DataFrame ?

  • To create a pandas object you need to use pandas.DataFrame( ) method.
  • Let us understand in details about it.

Syntax:

pandas.DataFrame(data=None, index=None, columns=None, dtype=None, copy=None)

(1) data: ndarray (structured or homogeneous), Iterable, dict, or DataFrame
  • Data can contain Series, arrays, constants, data classes or list-like objects. If data is a dictionary, column order follows insertion order.
  • If you have these data as input, then you can use the Pandas DataFrame method to create a Pandas object.
  • If a dictionary contains a Series which have an index defined, it is aligned by its index.
(2) index: Index or array-like
  • Index to use for resulting frame. Will default to RangeIndex if no indexing information part of input data and no index is provided.
(3) columns: Index or array-like
  • Column labels to use for resulting frame when data does not have them, defaulting to RangeIndex(0, 1, 2, …, n). If data contains column labels, will perform column selection instead.
(4) dtype: dtype, default None
  • Data type to force. Only a single dtype is allowed. If None, infer.
(5) copy: bool or None, default None
  • Copy data from inputs. For dict data, the default of None behaves like copy=True. For DataFrame or 2d ndarray input, the default of None behaves like copy=False. If data is a dict containing one or more Series (possibly of different dtypes), copy=False will ensure that these inputs are not copied.

(3) Creating Pandas Object Using Dictionary.

Example-1: Constructing Student DataFrame

import pandas as pd
student = {'Name':['Subrat','Abhispa','Arpita','Anuradha','Namita'],
          'Roll_No':[100,101,102,103,104],
          'Subject':['Math','English','Science','History','Commerce'],
          'Mark':[95,88,76,73,93]}
student_object = pd.DataFrame(student)
student_object

Output:

Note:

  • First, you need to import pandas, Here I have imported and given an alias name as ‘pd’
  • Second, I have created a dictionary called ‘student’.
  • Third, I have called pd.DataFrame() to convert a dictionary to a DataFrame object.

Example-2: Constructing Bike DataFrame.

import pandas as pd
bikes = {'Name':['Ninja','Suzuki','KTM','BMW','Ducati'],
        'Country':['Japan','Japan','Austria','Germany','Italy'],
        'Price':[500000,600000,700000,800000,900000],
        'Milage':['30KMPL','35KMPL','34KMPL','25KMPL','20KMPL']}
bikes_object = pd.DataFrame(bikes)
bikes_object

Output:

Example-3: Constructing DataFrame from a dictionary including Series:

import pandas as pd
data = {'col1': [0, 1, 2, 3], 'col2': pd.Series([4, 5, 6, 7])}
data_object = pd.DataFrame(data)
data_object

Output:

Example-4: Constructing DataFrame from numpy ndarray:

import pandas as pd
import numpy as np
array = pd.DataFrame(np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]]),
                   columns=['a', 'b', 'c'])
array_object = pd.DataFrame(array)
array_object

Output:

Leave a Reply

Your email address will not be published. Required fields are marked *