Please note, this is a STATIC archive of website www.w3resource.com from 19 Jul 2022, cach3.com does not collect or store any user information, there is no "phishing" involved.
w3resource

Pandas Excel: Import sheet2 data from a given excel sheet data into a Pandas dataframe

Pandas: Excel Exercise-21 with Solution

Write a Pandas program to import sheet2 data from a given excel data (employee.xlsx ) into a Pandas dataframe. Go to Excel data

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
df = pd.read_excel('E:\employee.xlsx',sheet_name=1)
print(df)

Sample Output:

    emp_id first_name    last_name  hire_date
0      120    Matthew        Weiss 2004-07-18
1      121       Adam        Fripp 2005-04-10
2      122      Payam     Kaufling 2003-05-01
3      123     Shanta      Vollman 2005-10-10
4      124      Kevin      Mourgos 2007-11-16
5      125      Julia        Nayer 2005-07-16
6      126      Irene  Mikkilineni 2006-09-28
7      127      James       Landry 2007-01-14
8      128     Steven       Markle 2008-03-08
9      129      Laura       Bissot 2005-08-20
10     130      Mozhe     Atkinson 2005-10-30
11     131      James       Marlow 2005-02-16
12     132         TJ        Olson 2007-04-10
13     133      Jason       Mallin 2004-06-14
14     134    Michael       Rogers 2006-08-26
15     135         Ki          Gee 2007-12-12
16     136      Hazel   Philtanker 2008-02-06
17     137     Renske       Ladwig 2003-07-14
18     138    Stephen       Stiles 2005-10-26	                                       

Excel Data:

employee.xlsx:

Sheet-1


Sheet-2


Sheet-3


Have another way to solve this solution? Contribute your code (and comments) through Disqus.

Previous: Write a Pandas program to import given excel data (employee.xlsx ) into a Pandas dataframe and sort based on multiple given columns.
Next: Write a Pandas program to import three datasheets from a given excel data (coalpublic2013.xlsx ) into a single dataframe.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Python: Tips of the Day

Find current directory and file's directory:

To get the full path to the directory a Python file is contained in, write this in that file:

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

(Note that the incantation above won't work if you've already used os.chdir() to change your current working directory, since the value of the __file__ constant is relative to the current working directory and is not changed by an os.chdir() call.)

To get the current working directory use

import os
cwd = os.getcwd()

Documentation references for the modules, constants and functions used above:

  • The os and os.path modules.
  • The __file__ constant
  • os.path.realpath(path) (returns "the canonical path of the specified filename, eliminating any symbolic links encountered in the path")
  • os.path.dirname(path) (returns "the directory name of pathname path")
  • os.getcwd() (returns "a string representing the current working directory")
  • os.chdir(path) ("change the current working directory to path")

Ref: https://bit.ly/3fy0R6m