Cannot compare types 'ndarray(dtype=int64)' and 'str'
Example of data that I want to replace

Data has following attributes
Buying v-high,high,med,low
Maint v-high,high,med,low
Doors 2,3,4,5-more
persons 2,4-more
lug_boot small,med,big
safety low,med.high
here is what I did
enter code here
#Buying price generalization
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
df["Buying_Price"]=df["Buying_Price"].replace({"high":3})
df["Buying_Price"]=df["Buying_Price"].replace({"med":2})
df["Buying_Price"]=df["Buying_Price"].replace({"low":1})
#Maintanace generalization
df["Maintanance_price"]=df["Maintanance_price"].replace({"vhigh":4})
df["Maintanance_price"]=df["Maintanance_price"].replace({"high":3})
df["Maintanance_price"]=df["Maintanance_price"].replace({"med":2})
df["Maintanance_price"]=df["Maintanance_price"].replace({"low":1})
#lug_boot generalization
df["Lug_boot"]=df["Lug_boot"].replace({"small":1})
df["Lug_boot"]=df["Lug_boot"].replace({"med":2})
df["Lug_boot"]=df["Lug_boot"].replace({"big":3})
#Safety Generalization
df["Safety"]=df["Safety"].replace({"low":1})
df["Safety"]=df["Safety"].replace({"med":2})
df["Safety"]=df["Safety"].replace({"big":3})
print(df.head())
while printing it showed "Cannot compare types 'ndarray(dtype=int64)' and 'str'"
python numpy
add a comment |
Example of data that I want to replace

Data has following attributes
Buying v-high,high,med,low
Maint v-high,high,med,low
Doors 2,3,4,5-more
persons 2,4-more
lug_boot small,med,big
safety low,med.high
here is what I did
enter code here
#Buying price generalization
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
df["Buying_Price"]=df["Buying_Price"].replace({"high":3})
df["Buying_Price"]=df["Buying_Price"].replace({"med":2})
df["Buying_Price"]=df["Buying_Price"].replace({"low":1})
#Maintanace generalization
df["Maintanance_price"]=df["Maintanance_price"].replace({"vhigh":4})
df["Maintanance_price"]=df["Maintanance_price"].replace({"high":3})
df["Maintanance_price"]=df["Maintanance_price"].replace({"med":2})
df["Maintanance_price"]=df["Maintanance_price"].replace({"low":1})
#lug_boot generalization
df["Lug_boot"]=df["Lug_boot"].replace({"small":1})
df["Lug_boot"]=df["Lug_boot"].replace({"med":2})
df["Lug_boot"]=df["Lug_boot"].replace({"big":3})
#Safety Generalization
df["Safety"]=df["Safety"].replace({"low":1})
df["Safety"]=df["Safety"].replace({"med":2})
df["Safety"]=df["Safety"].replace({"big":3})
print(df.head())
while printing it showed "Cannot compare types 'ndarray(dtype=int64)' and 'str'"
python numpy
df.headdoesn't do any computations on your data. Are you sure your error isn't some other line? Please provide full traceback.
– jpp
Nov 26 '18 at 10:19
Echo jpp's comment. Also you can considerably tidy your code by doing your replaces all at once using a dictionary:replace_all_these = {'low' : 1, 'med' : 2, 'high' : 3, 'vhigh' : 4 }; df['col'] = df['col'].replace(replace_all_these)
– T Burgis
Nov 26 '18 at 10:22
add a comment |
Example of data that I want to replace

Data has following attributes
Buying v-high,high,med,low
Maint v-high,high,med,low
Doors 2,3,4,5-more
persons 2,4-more
lug_boot small,med,big
safety low,med.high
here is what I did
enter code here
#Buying price generalization
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
df["Buying_Price"]=df["Buying_Price"].replace({"high":3})
df["Buying_Price"]=df["Buying_Price"].replace({"med":2})
df["Buying_Price"]=df["Buying_Price"].replace({"low":1})
#Maintanace generalization
df["Maintanance_price"]=df["Maintanance_price"].replace({"vhigh":4})
df["Maintanance_price"]=df["Maintanance_price"].replace({"high":3})
df["Maintanance_price"]=df["Maintanance_price"].replace({"med":2})
df["Maintanance_price"]=df["Maintanance_price"].replace({"low":1})
#lug_boot generalization
df["Lug_boot"]=df["Lug_boot"].replace({"small":1})
df["Lug_boot"]=df["Lug_boot"].replace({"med":2})
df["Lug_boot"]=df["Lug_boot"].replace({"big":3})
#Safety Generalization
df["Safety"]=df["Safety"].replace({"low":1})
df["Safety"]=df["Safety"].replace({"med":2})
df["Safety"]=df["Safety"].replace({"big":3})
print(df.head())
while printing it showed "Cannot compare types 'ndarray(dtype=int64)' and 'str'"
python numpy
Example of data that I want to replace

Data has following attributes
Buying v-high,high,med,low
Maint v-high,high,med,low
Doors 2,3,4,5-more
persons 2,4-more
lug_boot small,med,big
safety low,med.high
here is what I did
enter code here
#Buying price generalization
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
df["Buying_Price"]=df["Buying_Price"].replace({"high":3})
df["Buying_Price"]=df["Buying_Price"].replace({"med":2})
df["Buying_Price"]=df["Buying_Price"].replace({"low":1})
#Maintanace generalization
df["Maintanance_price"]=df["Maintanance_price"].replace({"vhigh":4})
df["Maintanance_price"]=df["Maintanance_price"].replace({"high":3})
df["Maintanance_price"]=df["Maintanance_price"].replace({"med":2})
df["Maintanance_price"]=df["Maintanance_price"].replace({"low":1})
#lug_boot generalization
df["Lug_boot"]=df["Lug_boot"].replace({"small":1})
df["Lug_boot"]=df["Lug_boot"].replace({"med":2})
df["Lug_boot"]=df["Lug_boot"].replace({"big":3})
#Safety Generalization
df["Safety"]=df["Safety"].replace({"low":1})
df["Safety"]=df["Safety"].replace({"med":2})
df["Safety"]=df["Safety"].replace({"big":3})
print(df.head())
while printing it showed "Cannot compare types 'ndarray(dtype=int64)' and 'str'"
python numpy
python numpy
asked Nov 26 '18 at 10:15
MiteshMitesh
5151515
5151515
df.headdoesn't do any computations on your data. Are you sure your error isn't some other line? Please provide full traceback.
– jpp
Nov 26 '18 at 10:19
Echo jpp's comment. Also you can considerably tidy your code by doing your replaces all at once using a dictionary:replace_all_these = {'low' : 1, 'med' : 2, 'high' : 3, 'vhigh' : 4 }; df['col'] = df['col'].replace(replace_all_these)
– T Burgis
Nov 26 '18 at 10:22
add a comment |
df.headdoesn't do any computations on your data. Are you sure your error isn't some other line? Please provide full traceback.
– jpp
Nov 26 '18 at 10:19
Echo jpp's comment. Also you can considerably tidy your code by doing your replaces all at once using a dictionary:replace_all_these = {'low' : 1, 'med' : 2, 'high' : 3, 'vhigh' : 4 }; df['col'] = df['col'].replace(replace_all_these)
– T Burgis
Nov 26 '18 at 10:22
df.head doesn't do any computations on your data. Are you sure your error isn't some other line? Please provide full traceback.– jpp
Nov 26 '18 at 10:19
df.head doesn't do any computations on your data. Are you sure your error isn't some other line? Please provide full traceback.– jpp
Nov 26 '18 at 10:19
Echo jpp's comment. Also you can considerably tidy your code by doing your replaces all at once using a dictionary:
replace_all_these = {'low' : 1, 'med' : 2, 'high' : 3, 'vhigh' : 4 }; df['col'] = df['col'].replace(replace_all_these)– T Burgis
Nov 26 '18 at 10:22
Echo jpp's comment. Also you can considerably tidy your code by doing your replaces all at once using a dictionary:
replace_all_these = {'low' : 1, 'med' : 2, 'high' : 3, 'vhigh' : 4 }; df['col'] = df['col'].replace(replace_all_these)– T Burgis
Nov 26 '18 at 10:22
add a comment |
1 Answer
1
active
oldest
votes
Some of you string you passed to replace with an (int)value, actually is an ndarray of int64 values.
You only have int64( here actually ndarray(dtype=int64)) type data in this column.
See document pandas.Dataframe.replace().
replace() try to seek and compare them with the str values you passed.
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
find all "vhigh" value and compare with the value currently contains, the replace it with 4.
At the comparing it fails as try to compare str data with int64 ('ndarray(dtype=int64)')
A brief example to simulate this:
import pandas as pd
import numpy as np
a = np.array([1])
df = pd.DataFrame({"Maintanance_price": a})
df["Maintanance_price"] = df["Maintanance_price"].replace({"a":1})
print(df)
Out:
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
found my problem, my code works fine and Thanks for the brief explanation .. The dataset has a space after 'Safety ' that was causing the problem
– Mitesh
Nov 26 '18 at 23:29
@Mitesh you're welcome and happy to your success.
– Geeocode
Nov 26 '18 at 23:58
add a comment |
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%2f53478932%2fcannot-compare-types-ndarraydtype-int64-and-str%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
Some of you string you passed to replace with an (int)value, actually is an ndarray of int64 values.
You only have int64( here actually ndarray(dtype=int64)) type data in this column.
See document pandas.Dataframe.replace().
replace() try to seek and compare them with the str values you passed.
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
find all "vhigh" value and compare with the value currently contains, the replace it with 4.
At the comparing it fails as try to compare str data with int64 ('ndarray(dtype=int64)')
A brief example to simulate this:
import pandas as pd
import numpy as np
a = np.array([1])
df = pd.DataFrame({"Maintanance_price": a})
df["Maintanance_price"] = df["Maintanance_price"].replace({"a":1})
print(df)
Out:
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
found my problem, my code works fine and Thanks for the brief explanation .. The dataset has a space after 'Safety ' that was causing the problem
– Mitesh
Nov 26 '18 at 23:29
@Mitesh you're welcome and happy to your success.
– Geeocode
Nov 26 '18 at 23:58
add a comment |
Some of you string you passed to replace with an (int)value, actually is an ndarray of int64 values.
You only have int64( here actually ndarray(dtype=int64)) type data in this column.
See document pandas.Dataframe.replace().
replace() try to seek and compare them with the str values you passed.
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
find all "vhigh" value and compare with the value currently contains, the replace it with 4.
At the comparing it fails as try to compare str data with int64 ('ndarray(dtype=int64)')
A brief example to simulate this:
import pandas as pd
import numpy as np
a = np.array([1])
df = pd.DataFrame({"Maintanance_price": a})
df["Maintanance_price"] = df["Maintanance_price"].replace({"a":1})
print(df)
Out:
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
found my problem, my code works fine and Thanks for the brief explanation .. The dataset has a space after 'Safety ' that was causing the problem
– Mitesh
Nov 26 '18 at 23:29
@Mitesh you're welcome and happy to your success.
– Geeocode
Nov 26 '18 at 23:58
add a comment |
Some of you string you passed to replace with an (int)value, actually is an ndarray of int64 values.
You only have int64( here actually ndarray(dtype=int64)) type data in this column.
See document pandas.Dataframe.replace().
replace() try to seek and compare them with the str values you passed.
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
find all "vhigh" value and compare with the value currently contains, the replace it with 4.
At the comparing it fails as try to compare str data with int64 ('ndarray(dtype=int64)')
A brief example to simulate this:
import pandas as pd
import numpy as np
a = np.array([1])
df = pd.DataFrame({"Maintanance_price": a})
df["Maintanance_price"] = df["Maintanance_price"].replace({"a":1})
print(df)
Out:
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
Some of you string you passed to replace with an (int)value, actually is an ndarray of int64 values.
You only have int64( here actually ndarray(dtype=int64)) type data in this column.
See document pandas.Dataframe.replace().
replace() try to seek and compare them with the str values you passed.
df["Buying_Price"]=df["Buying_Price"].replace({"vhigh":4})
find all "vhigh" value and compare with the value currently contains, the replace it with 4.
At the comparing it fails as try to compare str data with int64 ('ndarray(dtype=int64)')
A brief example to simulate this:
import pandas as pd
import numpy as np
a = np.array([1])
df = pd.DataFrame({"Maintanance_price": a})
df["Maintanance_price"] = df["Maintanance_price"].replace({"a":1})
print(df)
Out:
TypeError: Cannot compare types 'ndarray(dtype=int64)' and 'str'
edited Nov 26 '18 at 10:49
answered Nov 26 '18 at 10:32
GeeocodeGeeocode
2,41011021
2,41011021
found my problem, my code works fine and Thanks for the brief explanation .. The dataset has a space after 'Safety ' that was causing the problem
– Mitesh
Nov 26 '18 at 23:29
@Mitesh you're welcome and happy to your success.
– Geeocode
Nov 26 '18 at 23:58
add a comment |
found my problem, my code works fine and Thanks for the brief explanation .. The dataset has a space after 'Safety ' that was causing the problem
– Mitesh
Nov 26 '18 at 23:29
@Mitesh you're welcome and happy to your success.
– Geeocode
Nov 26 '18 at 23:58
found my problem, my code works fine and Thanks for the brief explanation .. The dataset has a space after 'Safety ' that was causing the problem
– Mitesh
Nov 26 '18 at 23:29
found my problem, my code works fine and Thanks for the brief explanation .. The dataset has a space after 'Safety ' that was causing the problem
– Mitesh
Nov 26 '18 at 23:29
@Mitesh you're welcome and happy to your success.
– Geeocode
Nov 26 '18 at 23:58
@Mitesh you're welcome and happy to your success.
– Geeocode
Nov 26 '18 at 23:58
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%2f53478932%2fcannot-compare-types-ndarraydtype-int64-and-str%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

df.headdoesn't do any computations on your data. Are you sure your error isn't some other line? Please provide full traceback.– jpp
Nov 26 '18 at 10:19
Echo jpp's comment. Also you can considerably tidy your code by doing your replaces all at once using a dictionary:
replace_all_these = {'low' : 1, 'med' : 2, 'high' : 3, 'vhigh' : 4 }; df['col'] = df['col'].replace(replace_all_these)– T Burgis
Nov 26 '18 at 10:22