w3resource

Pandas Excel: Read specific columns from a given excel file

Pandas: Excel Exercise-3 with Solution

Write a Pandas program to read specific columns from a given excel file. Go to Excel data

Sample Solution:

Python Code :

import pandas as pd
import numpy as np
cols = [1, 2, 4]
df = pd.read_excel('E:\coalpublic2013.xlsx', usecols=cols)
df

Sample Output:

    MSHA ID                       Mine_Name  Labor_Hours
0    103381            Tacoa Highwall Miner        22392
1    103404                Reid School Mine        28447
2    100759  North River #1 Underground Min       474784
3    103246                      Bear Creek        29193
4    103451                     Knight Mine        46393
5    103433              Crane Central Mine        47195
6    100329                    Concord Mine       144002
7    100851                  Oak Grove Mine      1001809
8    102901                Shoal Creek Mine        12396
9    102901                Shoal Creek Mine      1237415
10   103180             Sloan Mountain Mine       196963
11   103182                        Fishtrap        87314
12   103285                     Narley Mine        90584
13   103332                   Powhatan Mine        61394
14   103375                    Johnson Mine         1900
15   103419               Maxine-Pratt Mine       107469
16   103432                   Skelton Creek          220
17   103437         Black Warrior Mine No 1        70926
18   102976   Piney Woods Preparation Plant        14828
19   102976   Piney Woods Preparation Plant        23193
20   103380                          Calera        12621
21   103380                          Calera         1402
22   103422                 Clark No 1 Mine       140250
23   103467             Helena Surface Mine        30539
24   101247                       No 4 Mine      1551141
25   101401                       No 7 Mine      2464719
26   103172  Searles Mine No. 2, 3, 4, 5, 6       119542
27   103179             Fleetwood Mine No 1        63745
28   103303                    Shannon Mine       164388
29   103323                   Deerlick Mine        46381
30   103364           Brc Alabama No. 7 Llc        14324
31   103436                Swann's Crossing        77190
32   100347                    Choctaw Mine       215295
33   101362                 Manchester Mine       116914
34   102996                  Jap Creek Mine       164093
35   103155              Corinth Prep Plant        27996
36   103155              Corinth Prep Plant        51994
37   103195     Mccollum/Sparks Branch Mine        17411
38   103342             Reese's Branch Mine       115123
39   103370             Cresent Valley Mine          621
40   103372                 Cane Creek Mine        32401
41   103376                      Town Creek       176499
42   103389                Carbon Hill Mine        84966
43   103410                Coal Valley Mine       158591
44   103423                Dutton Hill Mine         9162
45  1519322                         Ghm #25         3108
46   103321                  Poplar Springs        76366
47   103358                       Old Union       161805
48  5000030                        Usibelli       286079
49   201195                    Kayenta Mine      1015333	                                       

Excel Data:

coalpublic2013.xlsx:


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

Previous: Write a Pandas program to get the data types of the given excel data (coalpublic2013.xlsx ) fields.
Next: Write a Pandas program to find the sum, mean, max, min value of 'Production (short tons)' column of coalpublic2013.xlsx file.

What is the difficulty level of this exercise?

Test your Programming skills with w3resource's quiz.



Share this Tutorial / Exercise on : Facebook and Twitter

Python: Tips of the Day

Split a String:

>>> sentence = 'this is, a python, tutorial, about, idioms.'
>>> sentence.split(', ')
['this is', 'a python', 'tutorial', 'about', 'idioms.']
>>> sentence.split(', ', 2)
['this is', 'a python', 'tutorial, about, idioms.']
>>> sentence.rsplit(', ')
['this is', 'a python', 'tutorial', 'about', 'idioms.']
>>> sentence.rsplit(', ', 2)
['this is, a python, tutorial', 'about', 'idioms.']