Python 3.6.5 Pandas Excel - Find tabular data in excel sheets
I need to extract only the tabular data from some excel books with multiple sheets that do not conform to any standard layout. I could write some code that loops over the rows and columns to find a table (find 2 or more contiguous cells/rows) but was hoping there may be a better way. I am implementing this in Python (v3.6.5) Pandas.
Sample scenarios:
Tabular data randomly placed in sheet - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank blank blank blank blank
blank blank blank blank blank
blank Col1 Col2 Col3 Col4
blank Row1 Row1 Row1 Row1
blank Row2 Row2 Row2 Row2
blank Row3 Row3 Row3 Row3
blank Row4 Row4 Row4 Row4
blank Row5 Row5 Row5 Row5
blank Row6 Row6 Row6 Row6
blank Row7 Row7 Row7 Row7
blank Row8 Row8 Row8 Row8
Tabular data starts at rows 3-6 - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank
blank
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
Tabular data starts after merged cells- I want to step over the merged cells and extract only the 'Col' and 'Row' data.
A B C D E
Col1 Col2 Col3 Col4
data in merged cells (A thru D)
data in merged cells (A thru D)
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
A B C D E
some data
some data
some data
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
EDIT
This is what I have do far:
import pandas as pd
import os
directory = '[path]'
for filename in os.listdir(directory):
if filename.endswith(".xlsx") or filename.endswith(".xlsm") or filename.endswith(".xls"):
df = pd.DataFrame()
xl = pd.ExcelFile(filename)
# print xl.sheet_names
sheets = xl.sheet_names
print()
print(filename)
for sheet in sheets:
df = pd.read_excel(filename, sheet)
#TO DO: extract tabular data from df
python excel pandas
add a comment |
I need to extract only the tabular data from some excel books with multiple sheets that do not conform to any standard layout. I could write some code that loops over the rows and columns to find a table (find 2 or more contiguous cells/rows) but was hoping there may be a better way. I am implementing this in Python (v3.6.5) Pandas.
Sample scenarios:
Tabular data randomly placed in sheet - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank blank blank blank blank
blank blank blank blank blank
blank Col1 Col2 Col3 Col4
blank Row1 Row1 Row1 Row1
blank Row2 Row2 Row2 Row2
blank Row3 Row3 Row3 Row3
blank Row4 Row4 Row4 Row4
blank Row5 Row5 Row5 Row5
blank Row6 Row6 Row6 Row6
blank Row7 Row7 Row7 Row7
blank Row8 Row8 Row8 Row8
Tabular data starts at rows 3-6 - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank
blank
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
Tabular data starts after merged cells- I want to step over the merged cells and extract only the 'Col' and 'Row' data.
A B C D E
Col1 Col2 Col3 Col4
data in merged cells (A thru D)
data in merged cells (A thru D)
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
A B C D E
some data
some data
some data
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
EDIT
This is what I have do far:
import pandas as pd
import os
directory = '[path]'
for filename in os.listdir(directory):
if filename.endswith(".xlsx") or filename.endswith(".xlsm") or filename.endswith(".xls"):
df = pd.DataFrame()
xl = pd.ExcelFile(filename)
# print xl.sheet_names
sheets = xl.sheet_names
print()
print(filename)
for sheet in sheets:
df = pd.read_excel(filename, sheet)
#TO DO: extract tabular data from df
python excel pandas
1
What have you tried so far? Can you show a minimal code (even if it doesn't work)?
– ma3oun
Nov 25 '18 at 20:20
sure, thanks ma3oun
– fdsfds dsfdsfsdf
Nov 25 '18 at 20:59
please provide a 'mcve': stackoverflow.com/help/mcve
– Zanshin
Nov 26 '18 at 11:18
I was able to resolve the problem on my own. thanks
– fdsfds dsfdsfsdf
Nov 28 '18 at 15:01
add a comment |
I need to extract only the tabular data from some excel books with multiple sheets that do not conform to any standard layout. I could write some code that loops over the rows and columns to find a table (find 2 or more contiguous cells/rows) but was hoping there may be a better way. I am implementing this in Python (v3.6.5) Pandas.
Sample scenarios:
Tabular data randomly placed in sheet - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank blank blank blank blank
blank blank blank blank blank
blank Col1 Col2 Col3 Col4
blank Row1 Row1 Row1 Row1
blank Row2 Row2 Row2 Row2
blank Row3 Row3 Row3 Row3
blank Row4 Row4 Row4 Row4
blank Row5 Row5 Row5 Row5
blank Row6 Row6 Row6 Row6
blank Row7 Row7 Row7 Row7
blank Row8 Row8 Row8 Row8
Tabular data starts at rows 3-6 - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank
blank
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
Tabular data starts after merged cells- I want to step over the merged cells and extract only the 'Col' and 'Row' data.
A B C D E
Col1 Col2 Col3 Col4
data in merged cells (A thru D)
data in merged cells (A thru D)
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
A B C D E
some data
some data
some data
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
EDIT
This is what I have do far:
import pandas as pd
import os
directory = '[path]'
for filename in os.listdir(directory):
if filename.endswith(".xlsx") or filename.endswith(".xlsm") or filename.endswith(".xls"):
df = pd.DataFrame()
xl = pd.ExcelFile(filename)
# print xl.sheet_names
sheets = xl.sheet_names
print()
print(filename)
for sheet in sheets:
df = pd.read_excel(filename, sheet)
#TO DO: extract tabular data from df
python excel pandas
I need to extract only the tabular data from some excel books with multiple sheets that do not conform to any standard layout. I could write some code that loops over the rows and columns to find a table (find 2 or more contiguous cells/rows) but was hoping there may be a better way. I am implementing this in Python (v3.6.5) Pandas.
Sample scenarios:
Tabular data randomly placed in sheet - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank blank blank blank blank
blank blank blank blank blank
blank Col1 Col2 Col3 Col4
blank Row1 Row1 Row1 Row1
blank Row2 Row2 Row2 Row2
blank Row3 Row3 Row3 Row3
blank Row4 Row4 Row4 Row4
blank Row5 Row5 Row5 Row5
blank Row6 Row6 Row6 Row6
blank Row7 Row7 Row7 Row7
blank Row8 Row8 Row8 Row8
Tabular data starts at rows 3-6 - I want to extract only the 'Col' and 'Row' data.
A B C D E
blank
blank
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
Tabular data starts after merged cells- I want to step over the merged cells and extract only the 'Col' and 'Row' data.
A B C D E
Col1 Col2 Col3 Col4
data in merged cells (A thru D)
data in merged cells (A thru D)
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
A B C D E
some data
some data
some data
Col1 Col2 Col3 Col4
Row1 Row1 Row1 Row1
Row2 Row2 Row2 Row2
Row3 Row3 Row3 Row3
Row4 Row4 Row4 Row4
Row5 Row5 Row5 Row5
Row6 Row6 Row6 Row6
Row7 Row7 Row7 Row7
Row8 Row8 Row8 Row8
EDIT
This is what I have do far:
import pandas as pd
import os
directory = '[path]'
for filename in os.listdir(directory):
if filename.endswith(".xlsx") or filename.endswith(".xlsm") or filename.endswith(".xls"):
df = pd.DataFrame()
xl = pd.ExcelFile(filename)
# print xl.sheet_names
sheets = xl.sheet_names
print()
print(filename)
for sheet in sheets:
df = pd.read_excel(filename, sheet)
#TO DO: extract tabular data from df
python excel pandas
python excel pandas
edited Nov 25 '18 at 20:54
fdsfds dsfdsfsdf
asked Nov 25 '18 at 19:57
fdsfds dsfdsfsdffdsfds dsfdsfsdf
11
11
1
What have you tried so far? Can you show a minimal code (even if it doesn't work)?
– ma3oun
Nov 25 '18 at 20:20
sure, thanks ma3oun
– fdsfds dsfdsfsdf
Nov 25 '18 at 20:59
please provide a 'mcve': stackoverflow.com/help/mcve
– Zanshin
Nov 26 '18 at 11:18
I was able to resolve the problem on my own. thanks
– fdsfds dsfdsfsdf
Nov 28 '18 at 15:01
add a comment |
1
What have you tried so far? Can you show a minimal code (even if it doesn't work)?
– ma3oun
Nov 25 '18 at 20:20
sure, thanks ma3oun
– fdsfds dsfdsfsdf
Nov 25 '18 at 20:59
please provide a 'mcve': stackoverflow.com/help/mcve
– Zanshin
Nov 26 '18 at 11:18
I was able to resolve the problem on my own. thanks
– fdsfds dsfdsfsdf
Nov 28 '18 at 15:01
1
1
What have you tried so far? Can you show a minimal code (even if it doesn't work)?
– ma3oun
Nov 25 '18 at 20:20
What have you tried so far? Can you show a minimal code (even if it doesn't work)?
– ma3oun
Nov 25 '18 at 20:20
sure, thanks ma3oun
– fdsfds dsfdsfsdf
Nov 25 '18 at 20:59
sure, thanks ma3oun
– fdsfds dsfdsfsdf
Nov 25 '18 at 20:59
please provide a 'mcve': stackoverflow.com/help/mcve
– Zanshin
Nov 26 '18 at 11:18
please provide a 'mcve': stackoverflow.com/help/mcve
– Zanshin
Nov 26 '18 at 11:18
I was able to resolve the problem on my own. thanks
– fdsfds dsfdsfsdf
Nov 28 '18 at 15:01
I was able to resolve the problem on my own. thanks
– fdsfds dsfdsfsdf
Nov 28 '18 at 15:01
add a comment |
0
active
oldest
votes
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471339%2fpython-3-6-5-pandas-excel-find-tabular-data-in-excel-sheets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53471339%2fpython-3-6-5-pandas-excel-find-tabular-data-in-excel-sheets%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
1
What have you tried so far? Can you show a minimal code (even if it doesn't work)?
– ma3oun
Nov 25 '18 at 20:20
sure, thanks ma3oun
– fdsfds dsfdsfsdf
Nov 25 '18 at 20:59
please provide a 'mcve': stackoverflow.com/help/mcve
– Zanshin
Nov 26 '18 at 11:18
I was able to resolve the problem on my own. thanks
– fdsfds dsfdsfsdf
Nov 28 '18 at 15:01