Skip to content
geeksforgeeks
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Tutorials
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
  • Practice
    • Build your AI Agent
    • GfG 160
    • Problem of the Day
    • Practice Coding Problems
    • GfG SDE Sheet
  • Contests
    • Accenture Hackathon (Ending Soon!)
    • GfG Weekly [Rated Contest]
    • Job-A-Thon Hiring Challenge
    • All Contests and Events
  • Data preprocessing
  • Data Manipulation
  • Data Analysis using Pandas
  • EDA
  • Pandas Exercise
  • Pandas AI
  • Numpy
  • Matplotlib
  • Plotly
  • Data Analysis
  • Machine Learning
  • Data science
Open In App
Next Article:
Python | Pandas Series.data
Next article icon

Data Structures in Pandas

Last Updated : 18 Oct, 2021
Comments
Improve
Suggest changes
Like Article
Like
Report

Pandas is an open-source library that uses for working with relational or labeled data both easily and intuitively. It provides various data structures and operations for manipulating numerical data and time series. It offers a tool for cleaning and processes your data. It is the most popular Python library that is used for data analysis. In this article, We are going to learn about Pandas Data structure.

It supports two data structures:

  • Series
  • Dataframe

Series

Pandas is a one-dimensional labeled array and capable of holding data of any type (integer, string, float, python objects, etc.)

Syntax: pandas.Series(data=None, index=None, dtype=None, name=None, copy=False, fastpath=False)

Parameters:

  • data: array- Contains data stored in Series.
  • index: array-like or Index (1d)
  • dtype: str, numpy.dtype, or ExtensionDtype, optional
  • name: str, optional
  • copy: bool, default False

Example 1: Series holding the char data type.

Python3

import pandas as pd
  
# a simple char list
list = ['g', 'e', 'e', 'k', 's']
   
# create series form a char list
res = pd.Series(list)
print(res)
                      
                       

 
 

Output:


 


 

Example 2: Series holding the Int data type.

Python3

import pandas as pd
  
# a simple int list
list = [1,2,3,4,5]
   
# create series form a int list
res = pd.Series(list)
print(res)
                      
                       

 
 

Output:


 


 

Example 3: Series holding the dictionary.

Python3

import pandas as pd
 
dic = { 'Id': 1013, 'Name': 'MOhe',
       'State': 'Maniput','Age': 24}
 
res = pd.Series(dic)
print(res)
                      
                       

 
 

Output:


 

Dataframe


 

Pandas DataFrame is a two-dimensional size-mutable, potentially heterogeneous tabular data structure with labeled axes (rows and columns). A Data frame is a two-dimensional data structure, i.e., data is aligned in a tabular fashion in rows and columns like a spreadsheet or SQL table, or a dict of Series objects. . Pandas DataFrame consists of three principal components, the data, rows, and columns.

Creating a Pandas DataFrame

In the real world, a Pandas DataFrame will be created by loading the datasets from existing storage, storage can be SQL Database, CSV file, and Excel file. Pandas DataFrame can be created from the lists, dictionary, and from a list of dictionary etc. Dataframe can be created in different ways here are some ways by which we create a dataframe:

Example 1: DataFrame can be created using a single list or a list of lists.

Python3

# import pandas as pd
import pandas as pd
  
# list of strings
lst = ['Geeks', 'For', 'Geeks', 'is',
            'portal', 'for', 'Geeks']
  
# Calling DataFrame constructor on list
df = pd.DataFrame(lst)
display(df)
                      
                       

 
 

Output:


 


 

Example 2: Creating DataFrame from dict of ndarray/lists. 

 To create DataFrame from dict of narray/list, all the narray must be of same length. If index is passed then the length index should be equal to the length of arrays. If no index is passed, then by default, index will be range(n) where n is the array length.

Python3

# Python code demonstrate creating
# DataFrame from dict narray / lists
# By default addresses.
  
import pandas as pd
  
# initialise data of lists.
data = {'Name':['Tom', 'nick', 'krish', 'jack'],
        'Age':[20, 21, 19, 18]}
  
# Create DataFrame
df = pd.DataFrame(data)
  
# Print the output.
display(df)
                      
                       

 
 

Output:


 

Dealing with a column and row in DataFrame

Selection of column: In Order to select a column in Pandas DataFrame, we can either access the columns by calling them by their columns name.

Python3

# Import pandas package
import pandas as pd
   
# Define a dictionary containing employee data
data = {'Name':['Jai', 'Princi', 'Gaurav', 'Anuj'],
        'Age':[27, 24, 22, 32],
        'Address':['Delhi', 'Kanpur', 'Allahabad', 'Kannauj'],
        'Qualification':['Msc', 'MA', 'MCA', 'Phd']}
   
# Convert the dictionary into DataFrame 
df = pd.DataFrame(data)
   
# select two columns
print(df[['Name', 'Qualification']])
                      
                       

Output:

How to Select Rows and Column from Pandas DataFrame?

Example 1: Selecting rows.

pandas.DataFrame.loc is a function used to select rows from Pandas DataFrame based on the condition provided.

Syntax: df.loc[df[‘cname’] ‘condition’]

Parameters:

  • df: represents data frame
  • cname: represents column name
  • condition: represents condition on which rows has to be selected

Python3

# Importing pandas as pd
from pandas import DataFrame
   
# Creating a data frame
Data = {'Name': ['Mohe', 'Shyni', 'Parul', 'Sam'],
        'ID': [12, 43, 54, 32],
        'Place': ['Delhi', 'Kochi', 'Pune', 'Patna']
       }
   
df = DataFrame(Data, columns = ['Name', 'ID', 'Place'])
   
# Print original data frame
print("Original data frame:\n")
display(df)
   
# Selecting the product of Electronic Type
select_prod = df.loc[df['Name'] == 'Mohe']
   
print("\n")
   
# Print selected rows based on the condition
print("Selecting rows:\n")
display (select_prod)
                      
                       

Output:

Example 2: Selecting column.

Python3

# Importing pandas as pd
from pandas import DataFrame
   
# Creating a data frame
Data = {'Name': ['Mohe', 'Shyni', 'Parul', 'Sam'],
        'ID': [12, 43, 54, 32],
        'Place': ['Delhi', 'Kochi', 'Pune', 'Patna']
       }
   
df = DataFrame(Data, columns = ['Name', 'ID', 'Place'])
   
# Print original data frame
print("Original data frame:")
display(df)
   
print("Selected column: ")
display(df[['Name', 'ID']] )
                      
                       

 
 

Output:


 


 



Next Article
Python | Pandas Series.data
author
kumar_satyam
Improve
Article Tags :
  • Python
  • Python pandas-datatypes
  • Python-pandas
Practice Tags :
  • python

Similar Reads

  • DataFrame vs Series in Pandas
    Pandas is a widely-used Python library for data analysis that provides two essential data structures: Series and DataFrame. These structures are potent tools for handling and examining data, but they have different features and applications. In this article, we will explore the differences between S
    8 min read
  • Pandas dataframe.sort_index()
    Pandas is one of those packages and makes importing and analyzing data much easier. When working with DataFrames, Pandas is used for handling tabular data. Let's learn Pandas DataFrame sort_index() method, which is used to sort the DataFrame based on index or column labels. Pandas sort_index() funct
    3 min read
  • Creating a Pandas Series
    A Pandas Series is like a single column of data in a spreadsheet. It is a one-dimensional array that can hold many types of data such as numbers, words or even other Python objects. Each value in a Series is associated with an index, which makes data retrieval and manipulation easy. This article exp
    3 min read
  • Python | Pandas Series.data
    Python is a great language for doing data analysis, primarily because of the fantastic ecosystem of data-centric python packages. Pandas is one of those packages and makes importing and analyzing data much easier. Pandas series is a One-dimensional ndarray with axis labels. The labels need not be un
    2 min read
  • Count Values in Pandas Dataframe
    Counting values in Pandas dataframe is important for understanding the distribution of data, checking for missing values or summarizing data. In this article, we will learn various methods to count values in a Pandas DataFrame We will be using below dataframe to learn about various methods: [GFGTABS
    3 min read
  • Data Processing with Pandas
    Data Processing is an important part of any task that includes data-driven work. It helps us to provide meaningful insights from the data. As we know Python is a widely used programming language, and there are various libraries and tools available for data processing. In this article, we are going t
    10 min read
  • How to Set Cell Value in Pandas DataFrame?
    In this article, we will discuss how to set cell values in Pandas DataFrame in Python. Method 1: Set value for a particular cell in pandas using dataframe.at This method is used to set the value of an existing value or set a new record. [GFGTABS] Python3 # import pandas module import pandas as pd #
    2 min read
  • Pandas DataFrame index Property
    In Pandas we have names to identify columns but for identifying rows, we have indices. The index property in a pandas dataFrame allows to identify and access specific rows within dataset. Essentially, the index is a series of labels that uniquely identify each row in the DataFrame. These labels can
    6 min read
  • Streamlined Data Ingestion with Pandas
    Data Ingestion is the process of, transferring data, from varied sources to an approach, where it can be analyzed, archived, or utilized by an establishment. The usual steps, involved in this process, are drawing out data, from its current place, converting the data, and, finally loading it, in a lo
    9 min read
  • Python | Pandas dataframe.info()
    The `dataframe.info()` function in Pandas proves to be an invaluable tool for obtaining a succinct summary of a dataframe. This function is particularly useful during exploratory analysis, offering a quick and informative overview of the dataset. Leveraging `dataframe.info()` is an efficient way to
    4 min read
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences