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:
Adding New Variable to Pandas DataFrame
Next article icon

Creating views on Pandas DataFrame

Last Updated : 23 Jan, 2019
Comments
Improve
Suggest changes
Like Article
Like
Report
Many times while doing data analysis we are dealing with a large data set, having a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that purpose, let's see how we can create views on the Dataframe and select only those columns that we need and leave the rest. For link to the CSV file used in the code, click here. Solution #1: A set of columns in the DataFrame can be selected by dropping all those columns which are not needed. Python3 1==
# importing pandas as pd import pandas as pd  # Reading the csv file df = pd.read_csv("nba.csv")  # Print the dataframe print(df) 
Output : Now we will select all columns except the first three columns. Python3
# drop the first three columns df.drop(df.columns[[0, 1, 2]], axis = 1) 
Output : We can also use the names of the column to be dropped. Python3
# drop the 'Name', 'Team' and 'Number' columns df.drop(['Name', 'Team', 'Number'], axis = 1) 
Output :   Solution #2 : We can individually select all those columns which we need and leave out the rest. Python3 1==
# importing pandas as pd import pandas as pd  # Reading the csv file df = pd.read_csv("nba.csv")  # select the first three columns # and store the result in a new dataframe df_copy = df.iloc[:, 0:3]  # Print the new DataFrame df_copy 
Output : We can also select the columns in a random manner by passing a list to the DataFrame.iloc attribute. Python3
# select the first, third and sixth columns # and store the result in a new dataframe # The numbering of columns begins from 0 df_copy = df.iloc[:, [0, 2, 5]]  # Print the new DataFrame df_copy 
Output : Alternatively, we can also name the columns that we want to select. Python3
# Select the below listed columns df_copy = df[['Name', 'Number', 'College']]  # Print the new DataFrame df_copy 
Output :

Next Article
Adding New Variable to Pandas DataFrame

S

Shubham__Ranjan
Improve
Article Tags :
  • Python
  • Pandas
  • Python-pandas
  • Python pandas-dataFrame
  • AI-ML-DS With Python
Practice Tags :
  • python

Similar Reads

  • Creating views on Pandas DataFrame | Set - 2
    Prerequisite: Creating views on Pandas DataFrame | Set - 1 Many times while doing data analysis we are dealing with a large data set has a lot of attributes. All the attributes are not necessarily equally important. As a result, we want to work with only a set of columns in the dataframe. For that p
    2 min read
  • Creating a Pandas DataFrame
    Pandas DataFrame comes is a powerful tool that allows us to store and manipulate data in a structured way, similar to an Excel spreadsheet or a SQL table. A DataFrame is similar to a table with rows and columns. It helps in handling large amounts of data, performing calculations, filtering informati
    3 min read
  • Adding New Variable to Pandas DataFrame
    In this article let's learn how to add a new variable to pandas DataFrame using the assign() function and square brackets. Pandas is a Python package that offers various data structures and operations for manipulating numerical data and time series. It is mainly popular for importing and analyzing d
    3 min read
  • Create empty dataframe in Pandas
    The Pandas Dataframe is a structure that has data in the 2D format and labels with it. DataFrames are widely used in data science, machine learning, and other such places. DataFrames are the same as SQL tables or Excel sheets but these are faster in use.Empty DataFrame could be created with the help
    1 min read
  • Creating Pandas dataframe using list of lists
    In this article, we will explore the Creating Pandas data frame using a list of lists. A Pandas DataFrame is a versatile 2-dimensional labeled data structure with columns that can contain different data types. It is widely utilized as one of the most common objects in the Pandas library. There are v
    4 min read
  • Creating a Pandas dataframe using list of tuples
    A Pandas DataFrame is an important data structure used for organizing and analyzing data in Python. Converting a list of tuples into a DataFrame makes it easier to work with data. In this article we'll see ways to create a DataFrame from a list of tuples. 1. Using pd.DataFrame()The simplest method t
    2 min read
  • Convert JSON to Pandas DataFrame
    When working with data, it's common to encounter JSON (JavaScript Object Notation) files, which are widely used for storing and exchanging data. Pandas, a powerful data manipulation library in Python, provides a convenient way to convert JSON data into a Pandas data frame. In this article, we'll exp
    4 min read
  • Create a list from rows in Pandas dataframe
    Python lists are one of the most versatile data structures, offering a range of built-in functions for efficient data manipulation. When working with Pandas, we often need to extract entire rows from a DataFrame and store them in a list for further processing. Unlike columns, which are easily access
    5 min read
  • Different ways to create Pandas Dataframe
    It is the most commonly used Pandas object. The pd.DataFrame() function is used to create a DataFrame in Pandas. There are several ways to create a Pandas Dataframe in Python. Example: Creating a DataFrame from a Dictionary [GFGTABS] Python import pandas as pd # initialize data of lists. data = {
    7 min read
  • 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
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