Pandas/Jupyter - Using .contains() - Can only use .str accessor with string values












0















So I'm doing an assignment on Jupyter Notebook using pandas.



The point is to adjust a DF column that contains degree info on personnel. I need to replace degrees entrys(strings) with numbers. (1 = Highschool, 2 = Techinical, 3 = Graduate, 4 = Post-Graduate)



import numpy as np
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

formacao = pd.read_csv("bases/formacao.csv")
formacao['grau'] = formacao.degree
formacao.grau.fillna(0, inplace=True)
formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3
formacao.loc[formacao.grau.str.contains('Master|MBA|Mestrado|Pós|Post|Especialista|Specialist|Specialization|Especialização',case=False,na=False)] = 4
formacao.grau.unique()


That is my code, the problem is that sometimes it works. Sometimes it doesn't. I used this exact code earlier and got all the results not yet covered. Then started adding new strings and I got this error:



AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas


I closed jupyter and it works again. I change a single letter, and the same error.
Now, I know this makes no sense, but I can't deliver the assignment like that. Not only because it isn't done, but also, how can I know if the teacher will be able to run the code.
What can possibly be wrong?



Here is the full traceback:



---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-4bad11236a95> in <module>
1 formacao['grau'] = formacao.degree
2 formacao.grau.fillna(0, inplace=True)
----> 3 formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
4 formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
5 formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoregeneric.py in __getattr__(self, name)
4370 if (name in self._internal_names_set or name in self._metadata or
4371 name in self._accessors):
-> 4372 return object.__getattribute__(self, name)
4373 else:
4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoreaccessor.py in __get__(self, obj, cls)
131 # we're accessing the attribute of the class, i.e., Dataset.geo
132 return self._accessor
--> 133 accessor_obj = self._accessor(obj)
134 # Replace the property with the accessor object. Inspired by:
135 # http://www.pydanny.com/cached-property.html

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in __init__(self, data)
1893
1894 def __init__(self, data):
-> 1895 self._validate(data)
1896 self._is_categorical = is_categorical_dtype(data)
1897

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in _validate(data)
1915 # (instead of test for object dtype), but that isn't practical for
1916 # performance reasons until we have a str dtype (GH 9343)
-> 1917 raise AttributeError("Can only use .str accessor with string "
1918 "values, which use np.object_ dtype in "
1919 "pandas")









share|improve this question























  • Please provide a Minimal, Complete, and Verifiable example

    – juanpa.arrivillaga
    Nov 15 '18 at 21:44











  • I change a single letter, and the same error. <- We can't help unless you tell us exactly what you are doing here via a reproducible example.

    – jpp
    Nov 15 '18 at 21:46













  • @Not sure how I can reproduce this. I could

    – Anyone
    Nov 15 '18 at 23:29
















0















So I'm doing an assignment on Jupyter Notebook using pandas.



The point is to adjust a DF column that contains degree info on personnel. I need to replace degrees entrys(strings) with numbers. (1 = Highschool, 2 = Techinical, 3 = Graduate, 4 = Post-Graduate)



import numpy as np
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

formacao = pd.read_csv("bases/formacao.csv")
formacao['grau'] = formacao.degree
formacao.grau.fillna(0, inplace=True)
formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3
formacao.loc[formacao.grau.str.contains('Master|MBA|Mestrado|Pós|Post|Especialista|Specialist|Specialization|Especialização',case=False,na=False)] = 4
formacao.grau.unique()


That is my code, the problem is that sometimes it works. Sometimes it doesn't. I used this exact code earlier and got all the results not yet covered. Then started adding new strings and I got this error:



AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas


I closed jupyter and it works again. I change a single letter, and the same error.
Now, I know this makes no sense, but I can't deliver the assignment like that. Not only because it isn't done, but also, how can I know if the teacher will be able to run the code.
What can possibly be wrong?



Here is the full traceback:



---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-4bad11236a95> in <module>
1 formacao['grau'] = formacao.degree
2 formacao.grau.fillna(0, inplace=True)
----> 3 formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
4 formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
5 formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoregeneric.py in __getattr__(self, name)
4370 if (name in self._internal_names_set or name in self._metadata or
4371 name in self._accessors):
-> 4372 return object.__getattribute__(self, name)
4373 else:
4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoreaccessor.py in __get__(self, obj, cls)
131 # we're accessing the attribute of the class, i.e., Dataset.geo
132 return self._accessor
--> 133 accessor_obj = self._accessor(obj)
134 # Replace the property with the accessor object. Inspired by:
135 # http://www.pydanny.com/cached-property.html

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in __init__(self, data)
1893
1894 def __init__(self, data):
-> 1895 self._validate(data)
1896 self._is_categorical = is_categorical_dtype(data)
1897

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in _validate(data)
1915 # (instead of test for object dtype), but that isn't practical for
1916 # performance reasons until we have a str dtype (GH 9343)
-> 1917 raise AttributeError("Can only use .str accessor with string "
1918 "values, which use np.object_ dtype in "
1919 "pandas")









share|improve this question























  • Please provide a Minimal, Complete, and Verifiable example

    – juanpa.arrivillaga
    Nov 15 '18 at 21:44











  • I change a single letter, and the same error. <- We can't help unless you tell us exactly what you are doing here via a reproducible example.

    – jpp
    Nov 15 '18 at 21:46













  • @Not sure how I can reproduce this. I could

    – Anyone
    Nov 15 '18 at 23:29














0












0








0








So I'm doing an assignment on Jupyter Notebook using pandas.



The point is to adjust a DF column that contains degree info on personnel. I need to replace degrees entrys(strings) with numbers. (1 = Highschool, 2 = Techinical, 3 = Graduate, 4 = Post-Graduate)



import numpy as np
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

formacao = pd.read_csv("bases/formacao.csv")
formacao['grau'] = formacao.degree
formacao.grau.fillna(0, inplace=True)
formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3
formacao.loc[formacao.grau.str.contains('Master|MBA|Mestrado|Pós|Post|Especialista|Specialist|Specialization|Especialização',case=False,na=False)] = 4
formacao.grau.unique()


That is my code, the problem is that sometimes it works. Sometimes it doesn't. I used this exact code earlier and got all the results not yet covered. Then started adding new strings and I got this error:



AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas


I closed jupyter and it works again. I change a single letter, and the same error.
Now, I know this makes no sense, but I can't deliver the assignment like that. Not only because it isn't done, but also, how can I know if the teacher will be able to run the code.
What can possibly be wrong?



Here is the full traceback:



---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-4bad11236a95> in <module>
1 formacao['grau'] = formacao.degree
2 formacao.grau.fillna(0, inplace=True)
----> 3 formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
4 formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
5 formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoregeneric.py in __getattr__(self, name)
4370 if (name in self._internal_names_set or name in self._metadata or
4371 name in self._accessors):
-> 4372 return object.__getattribute__(self, name)
4373 else:
4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoreaccessor.py in __get__(self, obj, cls)
131 # we're accessing the attribute of the class, i.e., Dataset.geo
132 return self._accessor
--> 133 accessor_obj = self._accessor(obj)
134 # Replace the property with the accessor object. Inspired by:
135 # http://www.pydanny.com/cached-property.html

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in __init__(self, data)
1893
1894 def __init__(self, data):
-> 1895 self._validate(data)
1896 self._is_categorical = is_categorical_dtype(data)
1897

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in _validate(data)
1915 # (instead of test for object dtype), but that isn't practical for
1916 # performance reasons until we have a str dtype (GH 9343)
-> 1917 raise AttributeError("Can only use .str accessor with string "
1918 "values, which use np.object_ dtype in "
1919 "pandas")









share|improve this question














So I'm doing an assignment on Jupyter Notebook using pandas.



The point is to adjust a DF column that contains degree info on personnel. I need to replace degrees entrys(strings) with numbers. (1 = Highschool, 2 = Techinical, 3 = Graduate, 4 = Post-Graduate)



import numpy as np
import pandas as pd
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)

formacao = pd.read_csv("bases/formacao.csv")
formacao['grau'] = formacao.degree
formacao.grau.fillna(0, inplace=True)
formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3
formacao.loc[formacao.grau.str.contains('Master|MBA|Mestrado|Pós|Post|Especialista|Specialist|Specialization|Especialização',case=False,na=False)] = 4
formacao.grau.unique()


That is my code, the problem is that sometimes it works. Sometimes it doesn't. I used this exact code earlier and got all the results not yet covered. Then started adding new strings and I got this error:



AttributeError: Can only use .str accessor with string values, which use np.object_ dtype in pandas


I closed jupyter and it works again. I change a single letter, and the same error.
Now, I know this makes no sense, but I can't deliver the assignment like that. Not only because it isn't done, but also, how can I know if the teacher will be able to run the code.
What can possibly be wrong?



Here is the full traceback:



---------------------------------------------------------------------------
AttributeError Traceback (most recent call last)
<ipython-input-12-4bad11236a95> in <module>
1 formacao['grau'] = formacao.degree
2 formacao.grau.fillna(0, inplace=True)
----> 3 formacao.loc[formacao.grau.str.contains('Tecnico|Curso T|Technical|Técnico|Technician|Minor|Technologist',case=False,na=False)] = 2
4 formacao.loc[formacao.grau.str.contains('Undergraduate|High School|Ensino Médio|Ensino Medio|Cursando|Under graduate',case=False,na=False)] = 1
5 formacao.loc[formacao.grau.str.contains('Bachelor|Bacharel|Licenciatura|B.S.|College|Engenheiro|Engenharia|Graduate|Ciencia|Ciência|Science|Graduação',case=False,na=False)] = 3

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoregeneric.py in __getattr__(self, name)
4370 if (name in self._internal_names_set or name in self._metadata or
4371 name in self._accessors):
-> 4372 return object.__getattribute__(self, name)
4373 else:
4374 if self._info_axis._can_hold_identifiers_and_holds_name(name):

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascoreaccessor.py in __get__(self, obj, cls)
131 # we're accessing the attribute of the class, i.e., Dataset.geo
132 return self._accessor
--> 133 accessor_obj = self._accessor(obj)
134 # Replace the property with the accessor object. Inspired by:
135 # http://www.pydanny.com/cached-property.html

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in __init__(self, data)
1893
1894 def __init__(self, data):
-> 1895 self._validate(data)
1896 self._is_categorical = is_categorical_dtype(data)
1897

c:usersuserappdatalocalprogramspythonpython36libsite-packagespandascorestrings.py in _validate(data)
1915 # (instead of test for object dtype), but that isn't practical for
1916 # performance reasons until we have a str dtype (GH 9343)
-> 1917 raise AttributeError("Can only use .str accessor with string "
1918 "values, which use np.object_ dtype in "
1919 "pandas")






python pandas jupyter-notebook






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 15 '18 at 21:38









AnyoneAnyone

225




225













  • Please provide a Minimal, Complete, and Verifiable example

    – juanpa.arrivillaga
    Nov 15 '18 at 21:44











  • I change a single letter, and the same error. <- We can't help unless you tell us exactly what you are doing here via a reproducible example.

    – jpp
    Nov 15 '18 at 21:46













  • @Not sure how I can reproduce this. I could

    – Anyone
    Nov 15 '18 at 23:29



















  • Please provide a Minimal, Complete, and Verifiable example

    – juanpa.arrivillaga
    Nov 15 '18 at 21:44











  • I change a single letter, and the same error. <- We can't help unless you tell us exactly what you are doing here via a reproducible example.

    – jpp
    Nov 15 '18 at 21:46













  • @Not sure how I can reproduce this. I could

    – Anyone
    Nov 15 '18 at 23:29

















Please provide a Minimal, Complete, and Verifiable example

– juanpa.arrivillaga
Nov 15 '18 at 21:44





Please provide a Minimal, Complete, and Verifiable example

– juanpa.arrivillaga
Nov 15 '18 at 21:44













I change a single letter, and the same error. <- We can't help unless you tell us exactly what you are doing here via a reproducible example.

– jpp
Nov 15 '18 at 21:46







I change a single letter, and the same error. <- We can't help unless you tell us exactly what you are doing here via a reproducible example.

– jpp
Nov 15 '18 at 21:46















@Not sure how I can reproduce this. I could

– Anyone
Nov 15 '18 at 23:29





@Not sure how I can reproduce this. I could

– Anyone
Nov 15 '18 at 23:29












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
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328256%2fpandas-jupyter-using-contains-can-only-use-str-accessor-with-string-valu%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
















draft saved

draft discarded




















































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.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53328256%2fpandas-jupyter-using-contains-can-only-use-str-accessor-with-string-valu%23new-answer', 'question_page');
}
);

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







Popular posts from this blog

Guess what letter conforming each word

Port of Spain

Run scheduled task as local user group (not BUILTIN)