Pandas.read_excel: Blanks in string columns convert to floats, converting via str() produces string 'Nan'

Multi tool use
In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.
How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?
python pandas nan
|
show 1 more comment
In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.
How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?
python pandas nan
you can replace theNan
with''
– meW
Dec 28 '18 at 3:16
but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18
provide a sample data in your question
– meW
Dec 28 '18 at 3:24
1
Try:df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27
@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39
|
show 1 more comment
In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.
How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?
python pandas nan
In importing an excel file via read_excel, string columns containing blanks are read in as float. When I force them back to strings via str(), the blanks become the string 'Nan', such that when I specify df[df.column=='Nan'], the values are returned.
How can I revise my code to ensure that blanks within a column do not force the entire column to float, and that blanks in a string column are read in as ''?
python pandas nan
python pandas nan
asked Dec 28 '18 at 3:14
UBHDNNX
53
53
you can replace theNan
with''
– meW
Dec 28 '18 at 3:16
but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18
provide a sample data in your question
– meW
Dec 28 '18 at 3:24
1
Try:df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27
@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39
|
show 1 more comment
you can replace theNan
with''
– meW
Dec 28 '18 at 3:16
but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18
provide a sample data in your question
– meW
Dec 28 '18 at 3:24
1
Try:df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27
@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39
you can replace the
Nan
with ''
– meW
Dec 28 '18 at 3:16
you can replace the
Nan
with ''
– meW
Dec 28 '18 at 3:16
but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18
but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18
provide a sample data in your question
– meW
Dec 28 '18 at 3:24
provide a sample data in your question
– meW
Dec 28 '18 at 3:24
1
1
Try:
df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27
Try:
df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27
@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39
@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39
|
show 1 more comment
2 Answers
2
active
oldest
votes
Try filling NA values on read:
df = pd.read_excel(...).fillna('')
add a comment |
You don't actually have to convert to string. You can actually do a:
df[df.colname!=df.colname] #Returns True if it encounters a NaN
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%2f53953286%2fpandas-read-excel-blanks-in-string-columns-convert-to-floats-converting-via-st%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
Try filling NA values on read:
df = pd.read_excel(...).fillna('')
add a comment |
Try filling NA values on read:
df = pd.read_excel(...).fillna('')
add a comment |
Try filling NA values on read:
df = pd.read_excel(...).fillna('')
Try filling NA values on read:
df = pd.read_excel(...).fillna('')
answered Dec 28 '18 at 21:43


James
13.1k11532
13.1k11532
add a comment |
add a comment |
You don't actually have to convert to string. You can actually do a:
df[df.colname!=df.colname] #Returns True if it encounters a NaN
add a comment |
You don't actually have to convert to string. You can actually do a:
df[df.colname!=df.colname] #Returns True if it encounters a NaN
add a comment |
You don't actually have to convert to string. You can actually do a:
df[df.colname!=df.colname] #Returns True if it encounters a NaN
You don't actually have to convert to string. You can actually do a:
df[df.colname!=df.colname] #Returns True if it encounters a NaN
answered Dec 28 '18 at 3:50


ycx
1,13616
1,13616
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53953286%2fpandas-read-excel-blanks-in-string-columns-convert-to-floats-converting-via-st%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
U,ZdtvvD6ngx7hVZxItLiFSRwx,WMQ05B 710w5PiQoqhm,Fa6vsk i x3XoVL2 m8D,lWG,frP,rRbOb,nW,OaAGVk6UEUsdrcW7w9eeO
you can replace the
Nan
with''
– meW
Dec 28 '18 at 3:16
but what if the string could actually be Nan? Example: last name column; last name is Nan.
– UBHDNNX
Dec 28 '18 at 3:18
provide a sample data in your question
– meW
Dec 28 '18 at 3:24
1
Try:
df = pd.read_excel(...).fillna('')
– James
Dec 28 '18 at 3:27
@James that worked perfectly. Push it to an answer and I'll mark it correct.
– UBHDNNX
Dec 28 '18 at 3:39