How to read Excel data by column name in python using xlrd










1















I am trying to read the data of large excel file(almost 100000 row).
I am using 'xlrd Module' in python to fetch the data from excel.
I want to fetch data by column name(Cascade,Schedule Name,Market) instead of column number(0,1,2).
Because my excel columns are not fixed.
i know how to fetch data in case of fixed column.



enter image description here



here is the code by which i am fetching data from the excel for fixed column



import xlrd

file_location =r"C:UsersDesktopVision.xlsx"
workbook=xlrd.open_workbook(file_location)
sheet= workbook.sheet_by_index(0)
print(sheet.ncols,sheet.nrows,sheet.name,sheet.number)

for i in range(sheet.nrows):
flag = 0
for j in range(sheet.ncols):
value=sheet.cell(i,j).value


If anyone has any solution of this, kindly let me know



Thanks










share|improve this question
























  • Edit your Question and give an examples of "by column name instead of column number"

    – stovfl
    Nov 14 '18 at 21:07











  • i have made changes in my question.

    – George.S
    Nov 14 '18 at 21:21











  • can you show me how ?

    – George.S
    Nov 14 '18 at 21:31















1















I am trying to read the data of large excel file(almost 100000 row).
I am using 'xlrd Module' in python to fetch the data from excel.
I want to fetch data by column name(Cascade,Schedule Name,Market) instead of column number(0,1,2).
Because my excel columns are not fixed.
i know how to fetch data in case of fixed column.



enter image description here



here is the code by which i am fetching data from the excel for fixed column



import xlrd

file_location =r"C:UsersDesktopVision.xlsx"
workbook=xlrd.open_workbook(file_location)
sheet= workbook.sheet_by_index(0)
print(sheet.ncols,sheet.nrows,sheet.name,sheet.number)

for i in range(sheet.nrows):
flag = 0
for j in range(sheet.ncols):
value=sheet.cell(i,j).value


If anyone has any solution of this, kindly let me know



Thanks










share|improve this question
























  • Edit your Question and give an examples of "by column name instead of column number"

    – stovfl
    Nov 14 '18 at 21:07











  • i have made changes in my question.

    – George.S
    Nov 14 '18 at 21:21











  • can you show me how ?

    – George.S
    Nov 14 '18 at 21:31













1












1








1








I am trying to read the data of large excel file(almost 100000 row).
I am using 'xlrd Module' in python to fetch the data from excel.
I want to fetch data by column name(Cascade,Schedule Name,Market) instead of column number(0,1,2).
Because my excel columns are not fixed.
i know how to fetch data in case of fixed column.



enter image description here



here is the code by which i am fetching data from the excel for fixed column



import xlrd

file_location =r"C:UsersDesktopVision.xlsx"
workbook=xlrd.open_workbook(file_location)
sheet= workbook.sheet_by_index(0)
print(sheet.ncols,sheet.nrows,sheet.name,sheet.number)

for i in range(sheet.nrows):
flag = 0
for j in range(sheet.ncols):
value=sheet.cell(i,j).value


If anyone has any solution of this, kindly let me know



Thanks










share|improve this question
















I am trying to read the data of large excel file(almost 100000 row).
I am using 'xlrd Module' in python to fetch the data from excel.
I want to fetch data by column name(Cascade,Schedule Name,Market) instead of column number(0,1,2).
Because my excel columns are not fixed.
i know how to fetch data in case of fixed column.



enter image description here



here is the code by which i am fetching data from the excel for fixed column



import xlrd

file_location =r"C:UsersDesktopVision.xlsx"
workbook=xlrd.open_workbook(file_location)
sheet= workbook.sheet_by_index(0)
print(sheet.ncols,sheet.nrows,sheet.name,sheet.number)

for i in range(sheet.nrows):
flag = 0
for j in range(sheet.ncols):
value=sheet.cell(i,j).value


If anyone has any solution of this, kindly let me know



Thanks







python excel python-3.x xlrd






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 15 '18 at 7:50







George.S

















asked Nov 14 '18 at 20:54









George.SGeorge.S

306




306












  • Edit your Question and give an examples of "by column name instead of column number"

    – stovfl
    Nov 14 '18 at 21:07











  • i have made changes in my question.

    – George.S
    Nov 14 '18 at 21:21











  • can you show me how ?

    – George.S
    Nov 14 '18 at 21:31

















  • Edit your Question and give an examples of "by column name instead of column number"

    – stovfl
    Nov 14 '18 at 21:07











  • i have made changes in my question.

    – George.S
    Nov 14 '18 at 21:21











  • can you show me how ?

    – George.S
    Nov 14 '18 at 21:31
















Edit your Question and give an examples of "by column name instead of column number"

– stovfl
Nov 14 '18 at 21:07





Edit your Question and give an examples of "by column name instead of column number"

– stovfl
Nov 14 '18 at 21:07













i have made changes in my question.

– George.S
Nov 14 '18 at 21:21





i have made changes in my question.

– George.S
Nov 14 '18 at 21:21













can you show me how ?

– George.S
Nov 14 '18 at 21:31





can you show me how ?

– George.S
Nov 14 '18 at 21:31












3 Answers
3






active

oldest

votes


















1















Comment: still not working when header of
fieldnames = ['Cascade', 'Market', 'Schedule', 'Name] and
Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal.




Keep order of fieldnames in col_idx, was not my initial goal.





Question: I want to fetch data by column name




The following OOP solution will work:



class OrderedByName():
"""
Privides a generator method, to iterate in Column Name ordered sequence
Provides subscription, to get columns index by name. using class[name]
"""
def __init__(self, sheet, fieldnames, row=0):
"""
Create a OrderedDict name:index from 'fieldnames'
:param sheet: The Worksheet to use
:param fieldnames: Ordered List of Column Names
:param row: Default Row Index for the Header Row
"""
from collections import OrderedDict
self.columns = OrderedDict().fromkeys(fieldnames, None)
for n in range(sheet.ncols):
self.columns[sheet.cell(row, n).value] = n

@property
def ncols(self):
"""
Generator, equal usage as range(xlrd.ncols),
to iterate columns in ordered sequence
:return: yield Column index
"""
for idx in self.columns.values():
yield idx

def __getitem__(self, item):
"""
Make class object subscriptable
:param item: Column Name
:return: Columns index
"""
return self.columns[item]



Usage:




# Worksheet Data
sheet([['Schedule', 'Cascade', 'Market'],
['SF05UB0', 'DO Macro Upgrade', 'Upper Cnetral Valley'],
['DE03HO0', 'DO Macro Upgrade', 'Toledo'],
['SF73XC4', 'DO Macro Upgrade', 'SF Bay']]
)

# Instantiate with Ordered List of Column Names
# NOTE the different Order of Column Names
by_name = OrderedByName(sheet, ['Cascade', 'Market', 'Schedule'])

# Iterate all Rows and all Columns Ordered as instantiated
for row in range(sheet.nrows):
for col in by_name.ncols:
value = sheet.cell(row, col).value
print("cell().value == ".format((row,col), value))




Output:



cell((0, 1)).value == Cascade
cell((0, 2)).value == Market
cell((0, 0)).value == Schedule
cell((1, 1)).value == DO Macro Upgrade
cell((1, 2)).value == Upper Cnetral Valley
cell((1, 0)).value == SF05UB0
cell((2, 1)).value == DO Macro Upgrade
cell((2, 2)).value == Toledo
cell((2, 0)).value == DE03HO0
cell((3, 1)).value == DO Macro Upgrade
cell((3, 2)).value == SF Bay
cell((3, 0)).value == SF73XC4






Get Index of one Column by Name



print("cell.value == ".format((1, by_name['Schedule']),
sheet.cell(1, by_name['Schedule']).value))
#>>> cell(1, 0).value == SF05UB0



Tested with Python: 3.5






share|improve this answer

























  • thanks stovfl for your answer but you are only printing the column index in but i want to print all the data of corresponding of column name. can you show me how i should use these column index for fetch correspondin row data of it

    – George.S
    Nov 15 '18 at 21:23












  • @George.S: From your Question: " i know how to fetch data in case of fixed column.". Edit you Question and show a not fixed data table and how you do this using the col_idx list.

    – stovfl
    Nov 15 '18 at 21:43











  • Actually " i know how to fetch data in case of fixed column." but problem is that in your code if i changed column header of my excel. it is print the column index in same order. so tell me how would my code knows that which header is residing in which column. i think i have cleared my point.

    – George.S
    Nov 16 '18 at 8:01












  • Thanks for update but it is still not working when header of fieldnames['Cascade', 'Market', 'Schedule', 'Name] and Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal. it is not showing the exact position of column header.

    – George.S
    Nov 16 '18 at 15:27



















2














Alternatively you could also make use of pandas, which is a comprehensive data analysis library with built-in excel I/O capabilities.



import pandas as pd

file_location =r"C:UsersesatnirDesktopSprint Vision.xlsx"

# Read out first sheet of excel file and return as pandas dataframe
df = pd.read_excel(file_location)

# Reduce dataframe to target columns (by filtering on column names)
df = df[['Cascade', 'Schedule Name', 'Market']]


where a quick view of the resulting dataframe df will show:



In [1]: df
Out[1]:
Cascade Schedule Name Market
0 SF05UB0 DO Macro Upgrade Upper Central Valley
1 DE03HO0 DO Macro Upgrade Toledo
2 SF73XC4 DO Macro Upgrade SF Bay





share|improve this answer























  • Thank Xukrao for your answer but i do not know how to perform operation over excel data using pandas. so i am unable to use it.

    – George.S
    Nov 16 '18 at 15:30


















2














Your column names are in the first row of the spreadsheet, right? So read the first row and construct a mapping from names to column indices.



column_pos = [ (sheet.cell(0, i).value, i) for i in range(sheet.ncols) ]
colidx = dict(column_pos)


Or as a one-liner:



colidx = dict( (sheet.cell(0, i).value, i) for i in range(sheet.ncols) )


You can then use the index to interpret column names, for example:



print(sheet.cell(5, colidx["Schedule Name"]).value)


To get an entire column, you can use a list comprehension:



schedule = [ sheet.cell(i, colidx["Schedule Name"]).value for i in range(1, sheet.nrows) ]


If you really wanted to, you could create a wrapper for the cell function that handles the interpretation. But I think this is simple enough.






share|improve this answer

























  • thanks Alexis for your answer. i want to fetch complete data of 'schedule name' instead of individual value. can you show me how ?

    – George.S
    Nov 15 '18 at 21:04











  • Done. (I assume row 0 contains the column names, so it's not included in the column values.)

    – alexis
    Nov 15 '18 at 21:51










Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308559%2fhow-to-read-excel-data-by-column-name-in-python-using-xlrd%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























3 Answers
3






active

oldest

votes








3 Answers
3






active

oldest

votes









active

oldest

votes






active

oldest

votes









1















Comment: still not working when header of
fieldnames = ['Cascade', 'Market', 'Schedule', 'Name] and
Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal.




Keep order of fieldnames in col_idx, was not my initial goal.





Question: I want to fetch data by column name




The following OOP solution will work:



class OrderedByName():
"""
Privides a generator method, to iterate in Column Name ordered sequence
Provides subscription, to get columns index by name. using class[name]
"""
def __init__(self, sheet, fieldnames, row=0):
"""
Create a OrderedDict name:index from 'fieldnames'
:param sheet: The Worksheet to use
:param fieldnames: Ordered List of Column Names
:param row: Default Row Index for the Header Row
"""
from collections import OrderedDict
self.columns = OrderedDict().fromkeys(fieldnames, None)
for n in range(sheet.ncols):
self.columns[sheet.cell(row, n).value] = n

@property
def ncols(self):
"""
Generator, equal usage as range(xlrd.ncols),
to iterate columns in ordered sequence
:return: yield Column index
"""
for idx in self.columns.values():
yield idx

def __getitem__(self, item):
"""
Make class object subscriptable
:param item: Column Name
:return: Columns index
"""
return self.columns[item]



Usage:




# Worksheet Data
sheet([['Schedule', 'Cascade', 'Market'],
['SF05UB0', 'DO Macro Upgrade', 'Upper Cnetral Valley'],
['DE03HO0', 'DO Macro Upgrade', 'Toledo'],
['SF73XC4', 'DO Macro Upgrade', 'SF Bay']]
)

# Instantiate with Ordered List of Column Names
# NOTE the different Order of Column Names
by_name = OrderedByName(sheet, ['Cascade', 'Market', 'Schedule'])

# Iterate all Rows and all Columns Ordered as instantiated
for row in range(sheet.nrows):
for col in by_name.ncols:
value = sheet.cell(row, col).value
print("cell().value == ".format((row,col), value))




Output:



cell((0, 1)).value == Cascade
cell((0, 2)).value == Market
cell((0, 0)).value == Schedule
cell((1, 1)).value == DO Macro Upgrade
cell((1, 2)).value == Upper Cnetral Valley
cell((1, 0)).value == SF05UB0
cell((2, 1)).value == DO Macro Upgrade
cell((2, 2)).value == Toledo
cell((2, 0)).value == DE03HO0
cell((3, 1)).value == DO Macro Upgrade
cell((3, 2)).value == SF Bay
cell((3, 0)).value == SF73XC4






Get Index of one Column by Name



print("cell.value == ".format((1, by_name['Schedule']),
sheet.cell(1, by_name['Schedule']).value))
#>>> cell(1, 0).value == SF05UB0



Tested with Python: 3.5






share|improve this answer

























  • thanks stovfl for your answer but you are only printing the column index in but i want to print all the data of corresponding of column name. can you show me how i should use these column index for fetch correspondin row data of it

    – George.S
    Nov 15 '18 at 21:23












  • @George.S: From your Question: " i know how to fetch data in case of fixed column.". Edit you Question and show a not fixed data table and how you do this using the col_idx list.

    – stovfl
    Nov 15 '18 at 21:43











  • Actually " i know how to fetch data in case of fixed column." but problem is that in your code if i changed column header of my excel. it is print the column index in same order. so tell me how would my code knows that which header is residing in which column. i think i have cleared my point.

    – George.S
    Nov 16 '18 at 8:01












  • Thanks for update but it is still not working when header of fieldnames['Cascade', 'Market', 'Schedule', 'Name] and Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal. it is not showing the exact position of column header.

    – George.S
    Nov 16 '18 at 15:27
















1















Comment: still not working when header of
fieldnames = ['Cascade', 'Market', 'Schedule', 'Name] and
Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal.




Keep order of fieldnames in col_idx, was not my initial goal.





Question: I want to fetch data by column name




The following OOP solution will work:



class OrderedByName():
"""
Privides a generator method, to iterate in Column Name ordered sequence
Provides subscription, to get columns index by name. using class[name]
"""
def __init__(self, sheet, fieldnames, row=0):
"""
Create a OrderedDict name:index from 'fieldnames'
:param sheet: The Worksheet to use
:param fieldnames: Ordered List of Column Names
:param row: Default Row Index for the Header Row
"""
from collections import OrderedDict
self.columns = OrderedDict().fromkeys(fieldnames, None)
for n in range(sheet.ncols):
self.columns[sheet.cell(row, n).value] = n

@property
def ncols(self):
"""
Generator, equal usage as range(xlrd.ncols),
to iterate columns in ordered sequence
:return: yield Column index
"""
for idx in self.columns.values():
yield idx

def __getitem__(self, item):
"""
Make class object subscriptable
:param item: Column Name
:return: Columns index
"""
return self.columns[item]



Usage:




# Worksheet Data
sheet([['Schedule', 'Cascade', 'Market'],
['SF05UB0', 'DO Macro Upgrade', 'Upper Cnetral Valley'],
['DE03HO0', 'DO Macro Upgrade', 'Toledo'],
['SF73XC4', 'DO Macro Upgrade', 'SF Bay']]
)

# Instantiate with Ordered List of Column Names
# NOTE the different Order of Column Names
by_name = OrderedByName(sheet, ['Cascade', 'Market', 'Schedule'])

# Iterate all Rows and all Columns Ordered as instantiated
for row in range(sheet.nrows):
for col in by_name.ncols:
value = sheet.cell(row, col).value
print("cell().value == ".format((row,col), value))




Output:



cell((0, 1)).value == Cascade
cell((0, 2)).value == Market
cell((0, 0)).value == Schedule
cell((1, 1)).value == DO Macro Upgrade
cell((1, 2)).value == Upper Cnetral Valley
cell((1, 0)).value == SF05UB0
cell((2, 1)).value == DO Macro Upgrade
cell((2, 2)).value == Toledo
cell((2, 0)).value == DE03HO0
cell((3, 1)).value == DO Macro Upgrade
cell((3, 2)).value == SF Bay
cell((3, 0)).value == SF73XC4






Get Index of one Column by Name



print("cell.value == ".format((1, by_name['Schedule']),
sheet.cell(1, by_name['Schedule']).value))
#>>> cell(1, 0).value == SF05UB0



Tested with Python: 3.5






share|improve this answer

























  • thanks stovfl for your answer but you are only printing the column index in but i want to print all the data of corresponding of column name. can you show me how i should use these column index for fetch correspondin row data of it

    – George.S
    Nov 15 '18 at 21:23












  • @George.S: From your Question: " i know how to fetch data in case of fixed column.". Edit you Question and show a not fixed data table and how you do this using the col_idx list.

    – stovfl
    Nov 15 '18 at 21:43











  • Actually " i know how to fetch data in case of fixed column." but problem is that in your code if i changed column header of my excel. it is print the column index in same order. so tell me how would my code knows that which header is residing in which column. i think i have cleared my point.

    – George.S
    Nov 16 '18 at 8:01












  • Thanks for update but it is still not working when header of fieldnames['Cascade', 'Market', 'Schedule', 'Name] and Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal. it is not showing the exact position of column header.

    – George.S
    Nov 16 '18 at 15:27














1












1








1








Comment: still not working when header of
fieldnames = ['Cascade', 'Market', 'Schedule', 'Name] and
Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal.




Keep order of fieldnames in col_idx, was not my initial goal.





Question: I want to fetch data by column name




The following OOP solution will work:



class OrderedByName():
"""
Privides a generator method, to iterate in Column Name ordered sequence
Provides subscription, to get columns index by name. using class[name]
"""
def __init__(self, sheet, fieldnames, row=0):
"""
Create a OrderedDict name:index from 'fieldnames'
:param sheet: The Worksheet to use
:param fieldnames: Ordered List of Column Names
:param row: Default Row Index for the Header Row
"""
from collections import OrderedDict
self.columns = OrderedDict().fromkeys(fieldnames, None)
for n in range(sheet.ncols):
self.columns[sheet.cell(row, n).value] = n

@property
def ncols(self):
"""
Generator, equal usage as range(xlrd.ncols),
to iterate columns in ordered sequence
:return: yield Column index
"""
for idx in self.columns.values():
yield idx

def __getitem__(self, item):
"""
Make class object subscriptable
:param item: Column Name
:return: Columns index
"""
return self.columns[item]



Usage:




# Worksheet Data
sheet([['Schedule', 'Cascade', 'Market'],
['SF05UB0', 'DO Macro Upgrade', 'Upper Cnetral Valley'],
['DE03HO0', 'DO Macro Upgrade', 'Toledo'],
['SF73XC4', 'DO Macro Upgrade', 'SF Bay']]
)

# Instantiate with Ordered List of Column Names
# NOTE the different Order of Column Names
by_name = OrderedByName(sheet, ['Cascade', 'Market', 'Schedule'])

# Iterate all Rows and all Columns Ordered as instantiated
for row in range(sheet.nrows):
for col in by_name.ncols:
value = sheet.cell(row, col).value
print("cell().value == ".format((row,col), value))




Output:



cell((0, 1)).value == Cascade
cell((0, 2)).value == Market
cell((0, 0)).value == Schedule
cell((1, 1)).value == DO Macro Upgrade
cell((1, 2)).value == Upper Cnetral Valley
cell((1, 0)).value == SF05UB0
cell((2, 1)).value == DO Macro Upgrade
cell((2, 2)).value == Toledo
cell((2, 0)).value == DE03HO0
cell((3, 1)).value == DO Macro Upgrade
cell((3, 2)).value == SF Bay
cell((3, 0)).value == SF73XC4






Get Index of one Column by Name



print("cell.value == ".format((1, by_name['Schedule']),
sheet.cell(1, by_name['Schedule']).value))
#>>> cell(1, 0).value == SF05UB0



Tested with Python: 3.5






share|improve this answer
















Comment: still not working when header of
fieldnames = ['Cascade', 'Market', 'Schedule', 'Name] and
Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal.




Keep order of fieldnames in col_idx, was not my initial goal.





Question: I want to fetch data by column name




The following OOP solution will work:



class OrderedByName():
"""
Privides a generator method, to iterate in Column Name ordered sequence
Provides subscription, to get columns index by name. using class[name]
"""
def __init__(self, sheet, fieldnames, row=0):
"""
Create a OrderedDict name:index from 'fieldnames'
:param sheet: The Worksheet to use
:param fieldnames: Ordered List of Column Names
:param row: Default Row Index for the Header Row
"""
from collections import OrderedDict
self.columns = OrderedDict().fromkeys(fieldnames, None)
for n in range(sheet.ncols):
self.columns[sheet.cell(row, n).value] = n

@property
def ncols(self):
"""
Generator, equal usage as range(xlrd.ncols),
to iterate columns in ordered sequence
:return: yield Column index
"""
for idx in self.columns.values():
yield idx

def __getitem__(self, item):
"""
Make class object subscriptable
:param item: Column Name
:return: Columns index
"""
return self.columns[item]



Usage:




# Worksheet Data
sheet([['Schedule', 'Cascade', 'Market'],
['SF05UB0', 'DO Macro Upgrade', 'Upper Cnetral Valley'],
['DE03HO0', 'DO Macro Upgrade', 'Toledo'],
['SF73XC4', 'DO Macro Upgrade', 'SF Bay']]
)

# Instantiate with Ordered List of Column Names
# NOTE the different Order of Column Names
by_name = OrderedByName(sheet, ['Cascade', 'Market', 'Schedule'])

# Iterate all Rows and all Columns Ordered as instantiated
for row in range(sheet.nrows):
for col in by_name.ncols:
value = sheet.cell(row, col).value
print("cell().value == ".format((row,col), value))




Output:



cell((0, 1)).value == Cascade
cell((0, 2)).value == Market
cell((0, 0)).value == Schedule
cell((1, 1)).value == DO Macro Upgrade
cell((1, 2)).value == Upper Cnetral Valley
cell((1, 0)).value == SF05UB0
cell((2, 1)).value == DO Macro Upgrade
cell((2, 2)).value == Toledo
cell((2, 0)).value == DE03HO0
cell((3, 1)).value == DO Macro Upgrade
cell((3, 2)).value == SF Bay
cell((3, 0)).value == SF73XC4






Get Index of one Column by Name



print("cell.value == ".format((1, by_name['Schedule']),
sheet.cell(1, by_name['Schedule']).value))
#>>> cell(1, 0).value == SF05UB0



Tested with Python: 3.5







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 17 '18 at 17:00

























answered Nov 14 '18 at 22:10









stovflstovfl

7,96031131




7,96031131












  • thanks stovfl for your answer but you are only printing the column index in but i want to print all the data of corresponding of column name. can you show me how i should use these column index for fetch correspondin row data of it

    – George.S
    Nov 15 '18 at 21:23












  • @George.S: From your Question: " i know how to fetch data in case of fixed column.". Edit you Question and show a not fixed data table and how you do this using the col_idx list.

    – stovfl
    Nov 15 '18 at 21:43











  • Actually " i know how to fetch data in case of fixed column." but problem is that in your code if i changed column header of my excel. it is print the column index in same order. so tell me how would my code knows that which header is residing in which column. i think i have cleared my point.

    – George.S
    Nov 16 '18 at 8:01












  • Thanks for update but it is still not working when header of fieldnames['Cascade', 'Market', 'Schedule', 'Name] and Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal. it is not showing the exact position of column header.

    – George.S
    Nov 16 '18 at 15:27


















  • thanks stovfl for your answer but you are only printing the column index in but i want to print all the data of corresponding of column name. can you show me how i should use these column index for fetch correspondin row data of it

    – George.S
    Nov 15 '18 at 21:23












  • @George.S: From your Question: " i know how to fetch data in case of fixed column.". Edit you Question and show a not fixed data table and how you do this using the col_idx list.

    – stovfl
    Nov 15 '18 at 21:43











  • Actually " i know how to fetch data in case of fixed column." but problem is that in your code if i changed column header of my excel. it is print the column index in same order. so tell me how would my code knows that which header is residing in which column. i think i have cleared my point.

    – George.S
    Nov 16 '18 at 8:01












  • Thanks for update but it is still not working when header of fieldnames['Cascade', 'Market', 'Schedule', 'Name] and Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal. it is not showing the exact position of column header.

    – George.S
    Nov 16 '18 at 15:27

















thanks stovfl for your answer but you are only printing the column index in but i want to print all the data of corresponding of column name. can you show me how i should use these column index for fetch correspondin row data of it

– George.S
Nov 15 '18 at 21:23






thanks stovfl for your answer but you are only printing the column index in but i want to print all the data of corresponding of column name. can you show me how i should use these column index for fetch correspondin row data of it

– George.S
Nov 15 '18 at 21:23














@George.S: From your Question: " i know how to fetch data in case of fixed column.". Edit you Question and show a not fixed data table and how you do this using the col_idx list.

– stovfl
Nov 15 '18 at 21:43





@George.S: From your Question: " i know how to fetch data in case of fixed column.". Edit you Question and show a not fixed data table and how you do this using the col_idx list.

– stovfl
Nov 15 '18 at 21:43













Actually " i know how to fetch data in case of fixed column." but problem is that in your code if i changed column header of my excel. it is print the column index in same order. so tell me how would my code knows that which header is residing in which column. i think i have cleared my point.

– George.S
Nov 16 '18 at 8:01






Actually " i know how to fetch data in case of fixed column." but problem is that in your code if i changed column header of my excel. it is print the column index in same order. so tell me how would my code knows that which header is residing in which column. i think i have cleared my point.

– George.S
Nov 16 '18 at 8:01














Thanks for update but it is still not working when header of fieldnames['Cascade', 'Market', 'Schedule', 'Name] and Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal. it is not showing the exact position of column header.

– George.S
Nov 16 '18 at 15:27






Thanks for update but it is still not working when header of fieldnames['Cascade', 'Market', 'Schedule', 'Name] and Sheet(['Cascade', 'Schedule', 'Name', 'Market']) are equal. it is not showing the exact position of column header.

– George.S
Nov 16 '18 at 15:27














2














Alternatively you could also make use of pandas, which is a comprehensive data analysis library with built-in excel I/O capabilities.



import pandas as pd

file_location =r"C:UsersesatnirDesktopSprint Vision.xlsx"

# Read out first sheet of excel file and return as pandas dataframe
df = pd.read_excel(file_location)

# Reduce dataframe to target columns (by filtering on column names)
df = df[['Cascade', 'Schedule Name', 'Market']]


where a quick view of the resulting dataframe df will show:



In [1]: df
Out[1]:
Cascade Schedule Name Market
0 SF05UB0 DO Macro Upgrade Upper Central Valley
1 DE03HO0 DO Macro Upgrade Toledo
2 SF73XC4 DO Macro Upgrade SF Bay





share|improve this answer























  • Thank Xukrao for your answer but i do not know how to perform operation over excel data using pandas. so i am unable to use it.

    – George.S
    Nov 16 '18 at 15:30















2














Alternatively you could also make use of pandas, which is a comprehensive data analysis library with built-in excel I/O capabilities.



import pandas as pd

file_location =r"C:UsersesatnirDesktopSprint Vision.xlsx"

# Read out first sheet of excel file and return as pandas dataframe
df = pd.read_excel(file_location)

# Reduce dataframe to target columns (by filtering on column names)
df = df[['Cascade', 'Schedule Name', 'Market']]


where a quick view of the resulting dataframe df will show:



In [1]: df
Out[1]:
Cascade Schedule Name Market
0 SF05UB0 DO Macro Upgrade Upper Central Valley
1 DE03HO0 DO Macro Upgrade Toledo
2 SF73XC4 DO Macro Upgrade SF Bay





share|improve this answer























  • Thank Xukrao for your answer but i do not know how to perform operation over excel data using pandas. so i am unable to use it.

    – George.S
    Nov 16 '18 at 15:30













2












2








2







Alternatively you could also make use of pandas, which is a comprehensive data analysis library with built-in excel I/O capabilities.



import pandas as pd

file_location =r"C:UsersesatnirDesktopSprint Vision.xlsx"

# Read out first sheet of excel file and return as pandas dataframe
df = pd.read_excel(file_location)

# Reduce dataframe to target columns (by filtering on column names)
df = df[['Cascade', 'Schedule Name', 'Market']]


where a quick view of the resulting dataframe df will show:



In [1]: df
Out[1]:
Cascade Schedule Name Market
0 SF05UB0 DO Macro Upgrade Upper Central Valley
1 DE03HO0 DO Macro Upgrade Toledo
2 SF73XC4 DO Macro Upgrade SF Bay





share|improve this answer













Alternatively you could also make use of pandas, which is a comprehensive data analysis library with built-in excel I/O capabilities.



import pandas as pd

file_location =r"C:UsersesatnirDesktopSprint Vision.xlsx"

# Read out first sheet of excel file and return as pandas dataframe
df = pd.read_excel(file_location)

# Reduce dataframe to target columns (by filtering on column names)
df = df[['Cascade', 'Schedule Name', 'Market']]


where a quick view of the resulting dataframe df will show:



In [1]: df
Out[1]:
Cascade Schedule Name Market
0 SF05UB0 DO Macro Upgrade Upper Central Valley
1 DE03HO0 DO Macro Upgrade Toledo
2 SF73XC4 DO Macro Upgrade SF Bay






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 14 '18 at 22:21









XukraoXukrao

2,4032829




2,4032829












  • Thank Xukrao for your answer but i do not know how to perform operation over excel data using pandas. so i am unable to use it.

    – George.S
    Nov 16 '18 at 15:30

















  • Thank Xukrao for your answer but i do not know how to perform operation over excel data using pandas. so i am unable to use it.

    – George.S
    Nov 16 '18 at 15:30
















Thank Xukrao for your answer but i do not know how to perform operation over excel data using pandas. so i am unable to use it.

– George.S
Nov 16 '18 at 15:30





Thank Xukrao for your answer but i do not know how to perform operation over excel data using pandas. so i am unable to use it.

– George.S
Nov 16 '18 at 15:30











2














Your column names are in the first row of the spreadsheet, right? So read the first row and construct a mapping from names to column indices.



column_pos = [ (sheet.cell(0, i).value, i) for i in range(sheet.ncols) ]
colidx = dict(column_pos)


Or as a one-liner:



colidx = dict( (sheet.cell(0, i).value, i) for i in range(sheet.ncols) )


You can then use the index to interpret column names, for example:



print(sheet.cell(5, colidx["Schedule Name"]).value)


To get an entire column, you can use a list comprehension:



schedule = [ sheet.cell(i, colidx["Schedule Name"]).value for i in range(1, sheet.nrows) ]


If you really wanted to, you could create a wrapper for the cell function that handles the interpretation. But I think this is simple enough.






share|improve this answer

























  • thanks Alexis for your answer. i want to fetch complete data of 'schedule name' instead of individual value. can you show me how ?

    – George.S
    Nov 15 '18 at 21:04











  • Done. (I assume row 0 contains the column names, so it's not included in the column values.)

    – alexis
    Nov 15 '18 at 21:51















2














Your column names are in the first row of the spreadsheet, right? So read the first row and construct a mapping from names to column indices.



column_pos = [ (sheet.cell(0, i).value, i) for i in range(sheet.ncols) ]
colidx = dict(column_pos)


Or as a one-liner:



colidx = dict( (sheet.cell(0, i).value, i) for i in range(sheet.ncols) )


You can then use the index to interpret column names, for example:



print(sheet.cell(5, colidx["Schedule Name"]).value)


To get an entire column, you can use a list comprehension:



schedule = [ sheet.cell(i, colidx["Schedule Name"]).value for i in range(1, sheet.nrows) ]


If you really wanted to, you could create a wrapper for the cell function that handles the interpretation. But I think this is simple enough.






share|improve this answer

























  • thanks Alexis for your answer. i want to fetch complete data of 'schedule name' instead of individual value. can you show me how ?

    – George.S
    Nov 15 '18 at 21:04











  • Done. (I assume row 0 contains the column names, so it's not included in the column values.)

    – alexis
    Nov 15 '18 at 21:51













2












2








2







Your column names are in the first row of the spreadsheet, right? So read the first row and construct a mapping from names to column indices.



column_pos = [ (sheet.cell(0, i).value, i) for i in range(sheet.ncols) ]
colidx = dict(column_pos)


Or as a one-liner:



colidx = dict( (sheet.cell(0, i).value, i) for i in range(sheet.ncols) )


You can then use the index to interpret column names, for example:



print(sheet.cell(5, colidx["Schedule Name"]).value)


To get an entire column, you can use a list comprehension:



schedule = [ sheet.cell(i, colidx["Schedule Name"]).value for i in range(1, sheet.nrows) ]


If you really wanted to, you could create a wrapper for the cell function that handles the interpretation. But I think this is simple enough.






share|improve this answer















Your column names are in the first row of the spreadsheet, right? So read the first row and construct a mapping from names to column indices.



column_pos = [ (sheet.cell(0, i).value, i) for i in range(sheet.ncols) ]
colidx = dict(column_pos)


Or as a one-liner:



colidx = dict( (sheet.cell(0, i).value, i) for i in range(sheet.ncols) )


You can then use the index to interpret column names, for example:



print(sheet.cell(5, colidx["Schedule Name"]).value)


To get an entire column, you can use a list comprehension:



schedule = [ sheet.cell(i, colidx["Schedule Name"]).value for i in range(1, sheet.nrows) ]


If you really wanted to, you could create a wrapper for the cell function that handles the interpretation. But I think this is simple enough.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 15 '18 at 21:51

























answered Nov 14 '18 at 22:23









alexisalexis

34.1k1056115




34.1k1056115












  • thanks Alexis for your answer. i want to fetch complete data of 'schedule name' instead of individual value. can you show me how ?

    – George.S
    Nov 15 '18 at 21:04











  • Done. (I assume row 0 contains the column names, so it's not included in the column values.)

    – alexis
    Nov 15 '18 at 21:51

















  • thanks Alexis for your answer. i want to fetch complete data of 'schedule name' instead of individual value. can you show me how ?

    – George.S
    Nov 15 '18 at 21:04











  • Done. (I assume row 0 contains the column names, so it's not included in the column values.)

    – alexis
    Nov 15 '18 at 21:51
















thanks Alexis for your answer. i want to fetch complete data of 'schedule name' instead of individual value. can you show me how ?

– George.S
Nov 15 '18 at 21:04





thanks Alexis for your answer. i want to fetch complete data of 'schedule name' instead of individual value. can you show me how ?

– George.S
Nov 15 '18 at 21:04













Done. (I assume row 0 contains the column names, so it's not included in the column values.)

– alexis
Nov 15 '18 at 21:51





Done. (I assume row 0 contains the column names, so it's not included in the column values.)

– alexis
Nov 15 '18 at 21:51

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53308559%2fhow-to-read-excel-data-by-column-name-in-python-using-xlrd%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







這個網誌中的熱門文章

How to read a connectionString WITH PROVIDER in .NET Core?

In R, how to develop a multiplot heatmap.2 figure showing key labels successfully

Museum of Modern and Contemporary Art of Trento and Rovereto