Reading and Passing Excel Filename with Pandas
I want to read excel file with Pandas, delete the header row and the first column and write the resultant data in an excel file with the same name. I want to do it for all the excel files in a folder. I have written the code for data reading and writing but having trouble with saving the data in a file with the same name. The code I have written is like this-
import numpy as np
import pandas as pd
import os
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel ('new.xlsx', skiprows=1)
df.drop (df.columns [0], axis=1, inplace=True)
df.to_csv ('new.csv', index=False)
How can I automate my code for all the excel files in the same folder?
python excel pandas
add a comment |
I want to read excel file with Pandas, delete the header row and the first column and write the resultant data in an excel file with the same name. I want to do it for all the excel files in a folder. I have written the code for data reading and writing but having trouble with saving the data in a file with the same name. The code I have written is like this-
import numpy as np
import pandas as pd
import os
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel ('new.xlsx', skiprows=1)
df.drop (df.columns [0], axis=1, inplace=True)
df.to_csv ('new.csv', index=False)
How can I automate my code for all the excel files in the same folder?
python excel pandas
add a comment |
I want to read excel file with Pandas, delete the header row and the first column and write the resultant data in an excel file with the same name. I want to do it for all the excel files in a folder. I have written the code for data reading and writing but having trouble with saving the data in a file with the same name. The code I have written is like this-
import numpy as np
import pandas as pd
import os
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel ('new.xlsx', skiprows=1)
df.drop (df.columns [0], axis=1, inplace=True)
df.to_csv ('new.csv', index=False)
How can I automate my code for all the excel files in the same folder?
python excel pandas
I want to read excel file with Pandas, delete the header row and the first column and write the resultant data in an excel file with the same name. I want to do it for all the excel files in a folder. I have written the code for data reading and writing but having trouble with saving the data in a file with the same name. The code I have written is like this-
import numpy as np
import pandas as pd
import os
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel ('new.xlsx', skiprows=1)
df.drop (df.columns [0], axis=1, inplace=True)
df.to_csv ('new.csv', index=False)
How can I automate my code for all the excel files in the same folder?
python excel pandas
python excel pandas
asked Jan 3 at 14:42
Soelem Aafnan YehamSoelem Aafnan Yeham
112
112
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Use variable filename
in function read_excel
and then create new file names by format
and for remove first column is possible use DataFrame.iloc
- select all columns without first:
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
Another solution with glob
, there is possible specify extensions:
import glob
for filename in glob.glob('./*.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
#python 3.6+
#df.iloc[:, 1:].to_csv (f'new_{filename}.csv', index=False)
great ans as always, BTW by any chance do you know any doc where one can find out abbreviations likef''
forformat
,r''
for regex, etc.
– meW
Jan 3 at 14:55
@meW - pure python is not easy for me, try ask it jpp :)
– jezrael
Jan 3 at 14:55
1
Okay will check with him.
– meW
Jan 3 at 14:56
Worked like a charm, thanks! Although, it is taking the whole name, including the .xlsx suffix, thus the name is being generated like new.xlsx.csv
– Soelem Aafnan Yeham
Jan 3 at 15:37
1
@Soelem Aafnan Yeham so add[:-5]
likedf.iloc[:, 1:].to_csv('new_{}.csv'.format(filename[:-5]), index=False)
for remove last 5 letters or usedf.iloc[:, 1:].to_csv('new_{}.csv'.format(os.path.splitext(filename)[0])), index=False)
– jezrael
Jan 3 at 16:22
add a comment |
Try Below for reading multiple files as follows:
import pandas as pd
import glob
# Read multiple files into one dataframe along with pandas `concat`
# if you have path defined like `/home/data/` then you can use `/home/data/*.xlsx` otherwise you directly mention the path.
df = pd.concat([pd.read_excel(files, sep=',', index=False, skiprows=1) for files in glob.glob("/home/data/*.xlsx")])
Alternative:
Read multiple files into one dataframe
all_Files = glob.glob('/home/data/*.xlsx')
df = pd.concat((pd.read_excel(files, sep=',', index=False, skiprows=1) for files in all_Files))
add a comment |
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%2f54024504%2freading-and-passing-excel-filename-with-pandas%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Use variable filename
in function read_excel
and then create new file names by format
and for remove first column is possible use DataFrame.iloc
- select all columns without first:
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
Another solution with glob
, there is possible specify extensions:
import glob
for filename in glob.glob('./*.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
#python 3.6+
#df.iloc[:, 1:].to_csv (f'new_{filename}.csv', index=False)
great ans as always, BTW by any chance do you know any doc where one can find out abbreviations likef''
forformat
,r''
for regex, etc.
– meW
Jan 3 at 14:55
@meW - pure python is not easy for me, try ask it jpp :)
– jezrael
Jan 3 at 14:55
1
Okay will check with him.
– meW
Jan 3 at 14:56
Worked like a charm, thanks! Although, it is taking the whole name, including the .xlsx suffix, thus the name is being generated like new.xlsx.csv
– Soelem Aafnan Yeham
Jan 3 at 15:37
1
@Soelem Aafnan Yeham so add[:-5]
likedf.iloc[:, 1:].to_csv('new_{}.csv'.format(filename[:-5]), index=False)
for remove last 5 letters or usedf.iloc[:, 1:].to_csv('new_{}.csv'.format(os.path.splitext(filename)[0])), index=False)
– jezrael
Jan 3 at 16:22
add a comment |
Use variable filename
in function read_excel
and then create new file names by format
and for remove first column is possible use DataFrame.iloc
- select all columns without first:
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
Another solution with glob
, there is possible specify extensions:
import glob
for filename in glob.glob('./*.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
#python 3.6+
#df.iloc[:, 1:].to_csv (f'new_{filename}.csv', index=False)
great ans as always, BTW by any chance do you know any doc where one can find out abbreviations likef''
forformat
,r''
for regex, etc.
– meW
Jan 3 at 14:55
@meW - pure python is not easy for me, try ask it jpp :)
– jezrael
Jan 3 at 14:55
1
Okay will check with him.
– meW
Jan 3 at 14:56
Worked like a charm, thanks! Although, it is taking the whole name, including the .xlsx suffix, thus the name is being generated like new.xlsx.csv
– Soelem Aafnan Yeham
Jan 3 at 15:37
1
@Soelem Aafnan Yeham so add[:-5]
likedf.iloc[:, 1:].to_csv('new_{}.csv'.format(filename[:-5]), index=False)
for remove last 5 letters or usedf.iloc[:, 1:].to_csv('new_{}.csv'.format(os.path.splitext(filename)[0])), index=False)
– jezrael
Jan 3 at 16:22
add a comment |
Use variable filename
in function read_excel
and then create new file names by format
and for remove first column is possible use DataFrame.iloc
- select all columns without first:
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
Another solution with glob
, there is possible specify extensions:
import glob
for filename in glob.glob('./*.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
#python 3.6+
#df.iloc[:, 1:].to_csv (f'new_{filename}.csv', index=False)
Use variable filename
in function read_excel
and then create new file names by format
and for remove first column is possible use DataFrame.iloc
- select all columns without first:
for filename in os.listdir ('./'):
if filename.endswith ('.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
Another solution with glob
, there is possible specify extensions:
import glob
for filename in glob.glob('./*.xlsx'):
df = pd.read_excel (filename, skiprows=1)
df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename), index=False)
#python 3.6+
#df.iloc[:, 1:].to_csv (f'new_{filename}.csv', index=False)
edited Jan 3 at 14:56
answered Jan 3 at 14:45
jezraeljezrael
353k26317391
353k26317391
great ans as always, BTW by any chance do you know any doc where one can find out abbreviations likef''
forformat
,r''
for regex, etc.
– meW
Jan 3 at 14:55
@meW - pure python is not easy for me, try ask it jpp :)
– jezrael
Jan 3 at 14:55
1
Okay will check with him.
– meW
Jan 3 at 14:56
Worked like a charm, thanks! Although, it is taking the whole name, including the .xlsx suffix, thus the name is being generated like new.xlsx.csv
– Soelem Aafnan Yeham
Jan 3 at 15:37
1
@Soelem Aafnan Yeham so add[:-5]
likedf.iloc[:, 1:].to_csv('new_{}.csv'.format(filename[:-5]), index=False)
for remove last 5 letters or usedf.iloc[:, 1:].to_csv('new_{}.csv'.format(os.path.splitext(filename)[0])), index=False)
– jezrael
Jan 3 at 16:22
add a comment |
great ans as always, BTW by any chance do you know any doc where one can find out abbreviations likef''
forformat
,r''
for regex, etc.
– meW
Jan 3 at 14:55
@meW - pure python is not easy for me, try ask it jpp :)
– jezrael
Jan 3 at 14:55
1
Okay will check with him.
– meW
Jan 3 at 14:56
Worked like a charm, thanks! Although, it is taking the whole name, including the .xlsx suffix, thus the name is being generated like new.xlsx.csv
– Soelem Aafnan Yeham
Jan 3 at 15:37
1
@Soelem Aafnan Yeham so add[:-5]
likedf.iloc[:, 1:].to_csv('new_{}.csv'.format(filename[:-5]), index=False)
for remove last 5 letters or usedf.iloc[:, 1:].to_csv('new_{}.csv'.format(os.path.splitext(filename)[0])), index=False)
– jezrael
Jan 3 at 16:22
great ans as always, BTW by any chance do you know any doc where one can find out abbreviations like
f''
for format
, r''
for regex, etc.– meW
Jan 3 at 14:55
great ans as always, BTW by any chance do you know any doc where one can find out abbreviations like
f''
for format
, r''
for regex, etc.– meW
Jan 3 at 14:55
@meW - pure python is not easy for me, try ask it jpp :)
– jezrael
Jan 3 at 14:55
@meW - pure python is not easy for me, try ask it jpp :)
– jezrael
Jan 3 at 14:55
1
1
Okay will check with him.
– meW
Jan 3 at 14:56
Okay will check with him.
– meW
Jan 3 at 14:56
Worked like a charm, thanks! Although, it is taking the whole name, including the .xlsx suffix, thus the name is being generated like new.xlsx.csv
– Soelem Aafnan Yeham
Jan 3 at 15:37
Worked like a charm, thanks! Although, it is taking the whole name, including the .xlsx suffix, thus the name is being generated like new.xlsx.csv
– Soelem Aafnan Yeham
Jan 3 at 15:37
1
1
@Soelem Aafnan Yeham so add
[:-5]
like df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename[:-5]), index=False)
for remove last 5 letters or use df.iloc[:, 1:].to_csv('new_{}.csv'.format(os.path.splitext(filename)[0])), index=False)
– jezrael
Jan 3 at 16:22
@Soelem Aafnan Yeham so add
[:-5]
like df.iloc[:, 1:].to_csv('new_{}.csv'.format(filename[:-5]), index=False)
for remove last 5 letters or use df.iloc[:, 1:].to_csv('new_{}.csv'.format(os.path.splitext(filename)[0])), index=False)
– jezrael
Jan 3 at 16:22
add a comment |
Try Below for reading multiple files as follows:
import pandas as pd
import glob
# Read multiple files into one dataframe along with pandas `concat`
# if you have path defined like `/home/data/` then you can use `/home/data/*.xlsx` otherwise you directly mention the path.
df = pd.concat([pd.read_excel(files, sep=',', index=False, skiprows=1) for files in glob.glob("/home/data/*.xlsx")])
Alternative:
Read multiple files into one dataframe
all_Files = glob.glob('/home/data/*.xlsx')
df = pd.concat((pd.read_excel(files, sep=',', index=False, skiprows=1) for files in all_Files))
add a comment |
Try Below for reading multiple files as follows:
import pandas as pd
import glob
# Read multiple files into one dataframe along with pandas `concat`
# if you have path defined like `/home/data/` then you can use `/home/data/*.xlsx` otherwise you directly mention the path.
df = pd.concat([pd.read_excel(files, sep=',', index=False, skiprows=1) for files in glob.glob("/home/data/*.xlsx")])
Alternative:
Read multiple files into one dataframe
all_Files = glob.glob('/home/data/*.xlsx')
df = pd.concat((pd.read_excel(files, sep=',', index=False, skiprows=1) for files in all_Files))
add a comment |
Try Below for reading multiple files as follows:
import pandas as pd
import glob
# Read multiple files into one dataframe along with pandas `concat`
# if you have path defined like `/home/data/` then you can use `/home/data/*.xlsx` otherwise you directly mention the path.
df = pd.concat([pd.read_excel(files, sep=',', index=False, skiprows=1) for files in glob.glob("/home/data/*.xlsx")])
Alternative:
Read multiple files into one dataframe
all_Files = glob.glob('/home/data/*.xlsx')
df = pd.concat((pd.read_excel(files, sep=',', index=False, skiprows=1) for files in all_Files))
Try Below for reading multiple files as follows:
import pandas as pd
import glob
# Read multiple files into one dataframe along with pandas `concat`
# if you have path defined like `/home/data/` then you can use `/home/data/*.xlsx` otherwise you directly mention the path.
df = pd.concat([pd.read_excel(files, sep=',', index=False, skiprows=1) for files in glob.glob("/home/data/*.xlsx")])
Alternative:
Read multiple files into one dataframe
all_Files = glob.glob('/home/data/*.xlsx')
df = pd.concat((pd.read_excel(files, sep=',', index=False, skiprows=1) for files in all_Files))
edited Jan 3 at 15:52
answered Jan 3 at 15:09
pygopygo
3,2211721
3,2211721
add a comment |
add a comment |
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%2f54024504%2freading-and-passing-excel-filename-with-pandas%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