Trying to change pandas column dtype from str to float
$begingroup$
I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:
data.loc[:,'column_name'] = [char.replace(',', '') for char in
data.loc[:,'column_name']]
but I am getting an error saying:
AttributeError: 'float' object has no attribute 'replace'.
The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.
python pandas
New contributor
$endgroup$
add a comment |
$begingroup$
I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:
data.loc[:,'column_name'] = [char.replace(',', '') for char in
data.loc[:,'column_name']]
but I am getting an error saying:
AttributeError: 'float' object has no attribute 'replace'.
The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.
python pandas
New contributor
$endgroup$
1
$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago
$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago
add a comment |
$begingroup$
I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:
data.loc[:,'column_name'] = [char.replace(',', '') for char in
data.loc[:,'column_name']]
but I am getting an error saying:
AttributeError: 'float' object has no attribute 'replace'.
The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.
python pandas
New contributor
$endgroup$
I am trying to convert a pandas column from a str to a float. Before I convert the strings to floats using astype(float), I need to remove characters from the string which cannot be converted into floats, like a comma or a space. In trying to remove these characters, I am using list compression and the str.replace method like this:
data.loc[:,'column_name'] = [char.replace(',', '') for char in
data.loc[:,'column_name']]
but I am getting an error saying:
AttributeError: 'float' object has no attribute 'replace'.
The confusing part, though, is that when I look at the dtypes, the dtype for that column shows up as 'o', or object. So I have no idea what is going on.
python pandas
python pandas
New contributor
New contributor
edited 15 hours ago
Siong Thye Goh
1,122418
1,122418
New contributor
asked 19 hours ago
SebastianSebastian
12
12
New contributor
New contributor
1
$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago
$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago
add a comment |
1
$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago
$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago
1
1
$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago
$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago
$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago
$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago
add a comment |
2 Answers
2
active
oldest
votes
$begingroup$
In pandas
the object
type is used when there is not a clear distinction between the types stored in the column.
So, I guess that in your column, some objects are float
type and some objects are str
type. Or maybe, you are also dealing with NaN
objects, NaN
objects are float
objects.
a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str
type or just make the type conversion of the column you are dealing with.
b) After that, you can apply the string changes and/or deal with the NaN objects.
c) Finally, you transform your column into float
type`.
$endgroup$
add a comment |
$begingroup$
Maybe it's a very rudimentary method but I would just do
listt =
for i in data['column_name']:
listt.append(float(i))
data['FloatData'] = listt
New contributor
$endgroup$
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
});
});
}, "mathjax-editing");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "557"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Sebastian is a new contributor. Be nice, and check out our Code of Conduct.
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%2fdatascience.stackexchange.com%2fquestions%2f45799%2ftrying-to-change-pandas-column-dtype-from-str-to-float%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
$begingroup$
In pandas
the object
type is used when there is not a clear distinction between the types stored in the column.
So, I guess that in your column, some objects are float
type and some objects are str
type. Or maybe, you are also dealing with NaN
objects, NaN
objects are float
objects.
a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str
type or just make the type conversion of the column you are dealing with.
b) After that, you can apply the string changes and/or deal with the NaN objects.
c) Finally, you transform your column into float
type`.
$endgroup$
add a comment |
$begingroup$
In pandas
the object
type is used when there is not a clear distinction between the types stored in the column.
So, I guess that in your column, some objects are float
type and some objects are str
type. Or maybe, you are also dealing with NaN
objects, NaN
objects are float
objects.
a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str
type or just make the type conversion of the column you are dealing with.
b) After that, you can apply the string changes and/or deal with the NaN objects.
c) Finally, you transform your column into float
type`.
$endgroup$
add a comment |
$begingroup$
In pandas
the object
type is used when there is not a clear distinction between the types stored in the column.
So, I guess that in your column, some objects are float
type and some objects are str
type. Or maybe, you are also dealing with NaN
objects, NaN
objects are float
objects.
a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str
type or just make the type conversion of the column you are dealing with.
b) After that, you can apply the string changes and/or deal with the NaN objects.
c) Finally, you transform your column into float
type`.
$endgroup$
In pandas
the object
type is used when there is not a clear distinction between the types stored in the column.
So, I guess that in your column, some objects are float
type and some objects are str
type. Or maybe, you are also dealing with NaN
objects, NaN
objects are float
objects.
a) Convert the column to string: Are you getting your DataFrame from a CSV or XLS format file? Then at the moment of reading the file, you can specify that that column is an str
type or just make the type conversion of the column you are dealing with.
b) After that, you can apply the string changes and/or deal with the NaN objects.
c) Finally, you transform your column into float
type`.
edited 18 hours ago
answered 18 hours ago
omaromar
16114
16114
add a comment |
add a comment |
$begingroup$
Maybe it's a very rudimentary method but I would just do
listt =
for i in data['column_name']:
listt.append(float(i))
data['FloatData'] = listt
New contributor
$endgroup$
add a comment |
$begingroup$
Maybe it's a very rudimentary method but I would just do
listt =
for i in data['column_name']:
listt.append(float(i))
data['FloatData'] = listt
New contributor
$endgroup$
add a comment |
$begingroup$
Maybe it's a very rudimentary method but I would just do
listt =
for i in data['column_name']:
listt.append(float(i))
data['FloatData'] = listt
New contributor
$endgroup$
Maybe it's a very rudimentary method but I would just do
listt =
for i in data['column_name']:
listt.append(float(i))
data['FloatData'] = listt
New contributor
New contributor
answered 15 hours ago
Armando DelgadoArmando Delgado
1
1
New contributor
New contributor
add a comment |
add a comment |
Sebastian is a new contributor. Be nice, and check out our Code of Conduct.
Sebastian is a new contributor. Be nice, and check out our Code of Conduct.
Sebastian is a new contributor. Be nice, and check out our Code of Conduct.
Sebastian is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Data Science Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fdatascience.stackexchange.com%2fquestions%2f45799%2ftrying-to-change-pandas-column-dtype-from-str-to-float%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
$begingroup$
look at set([type(x) for x in data['column_name'].values] - a common problem is that it has some NaN values which are float, not string.
$endgroup$
– Valentas
18 hours ago
$begingroup$
The question is about programming, not about Data Science, it is better if you move your question to StackOverflow.
$endgroup$
– omar
18 hours ago