Create new column in pandas df and assign string values conditionally





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty{ height:90px;width:728px;box-sizing:border-box;
}







2















I'm new to pandas, trying to create a new column in Pandas Dataframe, and assign a string value based on a function, but the outcome outputs only 1 value ('residential) to all 5,000 columns. Any idea what's wrong with my code? Thank you



def programType(c):
if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':
return 'Residential'

elif c['Primary Property Type - Self Selected'] == 'Bank Branch' or 'Hotel' or 'Financial Office'
or 'Retail Store' or 'Distribution Center' or 'Non-Refrigerated Warehouse' or 'Fitness Center/Health Club/Gym'
or 'Mixed Use Property' or 'Self-Storage Facility' or 'Wholesale Club/Supercenter' or 'Supermarket/Grocery Store':
return 'Commercial'

elif c['Primary Property Type - Self Selected'] == 'Senior Care Community' or 'K-12 School' or 'College/University'
or 'Worship Facility' or 'Medical Office' or 'Hospital (General Medical & Surgical)':
return 'Institutional'

elif c['Primary Property Type - Self Selected'] == 'Manufacturing/Industrial Plant':
return 'Industrial'

else:
return 'Other'


The new column is called 'Program Type'



datav3['Program Type'] = datav3.apply(programType, axis=1)









share|improve this question

























  • I am a bit curious - do you need loops (apply) for some reason?

    – jezrael
    Nov 29 '18 at 6:59











  • Nope, if there's a better syntax i'm happy to use it. I also just went through your answer, and understood it now. Thank you.

    – abigfatcat
    Dec 1 '18 at 3:45




















2















I'm new to pandas, trying to create a new column in Pandas Dataframe, and assign a string value based on a function, but the outcome outputs only 1 value ('residential) to all 5,000 columns. Any idea what's wrong with my code? Thank you



def programType(c):
if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':
return 'Residential'

elif c['Primary Property Type - Self Selected'] == 'Bank Branch' or 'Hotel' or 'Financial Office'
or 'Retail Store' or 'Distribution Center' or 'Non-Refrigerated Warehouse' or 'Fitness Center/Health Club/Gym'
or 'Mixed Use Property' or 'Self-Storage Facility' or 'Wholesale Club/Supercenter' or 'Supermarket/Grocery Store':
return 'Commercial'

elif c['Primary Property Type - Self Selected'] == 'Senior Care Community' or 'K-12 School' or 'College/University'
or 'Worship Facility' or 'Medical Office' or 'Hospital (General Medical & Surgical)':
return 'Institutional'

elif c['Primary Property Type - Self Selected'] == 'Manufacturing/Industrial Plant':
return 'Industrial'

else:
return 'Other'


The new column is called 'Program Type'



datav3['Program Type'] = datav3.apply(programType, axis=1)









share|improve this question

























  • I am a bit curious - do you need loops (apply) for some reason?

    – jezrael
    Nov 29 '18 at 6:59











  • Nope, if there's a better syntax i'm happy to use it. I also just went through your answer, and understood it now. Thank you.

    – abigfatcat
    Dec 1 '18 at 3:45
















2












2








2








I'm new to pandas, trying to create a new column in Pandas Dataframe, and assign a string value based on a function, but the outcome outputs only 1 value ('residential) to all 5,000 columns. Any idea what's wrong with my code? Thank you



def programType(c):
if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':
return 'Residential'

elif c['Primary Property Type - Self Selected'] == 'Bank Branch' or 'Hotel' or 'Financial Office'
or 'Retail Store' or 'Distribution Center' or 'Non-Refrigerated Warehouse' or 'Fitness Center/Health Club/Gym'
or 'Mixed Use Property' or 'Self-Storage Facility' or 'Wholesale Club/Supercenter' or 'Supermarket/Grocery Store':
return 'Commercial'

elif c['Primary Property Type - Self Selected'] == 'Senior Care Community' or 'K-12 School' or 'College/University'
or 'Worship Facility' or 'Medical Office' or 'Hospital (General Medical & Surgical)':
return 'Institutional'

elif c['Primary Property Type - Self Selected'] == 'Manufacturing/Industrial Plant':
return 'Industrial'

else:
return 'Other'


The new column is called 'Program Type'



datav3['Program Type'] = datav3.apply(programType, axis=1)









share|improve this question
















I'm new to pandas, trying to create a new column in Pandas Dataframe, and assign a string value based on a function, but the outcome outputs only 1 value ('residential) to all 5,000 columns. Any idea what's wrong with my code? Thank you



def programType(c):
if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':
return 'Residential'

elif c['Primary Property Type - Self Selected'] == 'Bank Branch' or 'Hotel' or 'Financial Office'
or 'Retail Store' or 'Distribution Center' or 'Non-Refrigerated Warehouse' or 'Fitness Center/Health Club/Gym'
or 'Mixed Use Property' or 'Self-Storage Facility' or 'Wholesale Club/Supercenter' or 'Supermarket/Grocery Store':
return 'Commercial'

elif c['Primary Property Type - Self Selected'] == 'Senior Care Community' or 'K-12 School' or 'College/University'
or 'Worship Facility' or 'Medical Office' or 'Hospital (General Medical & Surgical)':
return 'Institutional'

elif c['Primary Property Type - Self Selected'] == 'Manufacturing/Industrial Plant':
return 'Industrial'

else:
return 'Other'


The new column is called 'Program Type'



datav3['Program Type'] = datav3.apply(programType, axis=1)






python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 4 '18 at 12:12









Mayank Porwal

5,0182725




5,0182725










asked Nov 29 '18 at 6:37









abigfatcatabigfatcat

225




225













  • I am a bit curious - do you need loops (apply) for some reason?

    – jezrael
    Nov 29 '18 at 6:59











  • Nope, if there's a better syntax i'm happy to use it. I also just went through your answer, and understood it now. Thank you.

    – abigfatcat
    Dec 1 '18 at 3:45





















  • I am a bit curious - do you need loops (apply) for some reason?

    – jezrael
    Nov 29 '18 at 6:59











  • Nope, if there's a better syntax i'm happy to use it. I also just went through your answer, and understood it now. Thank you.

    – abigfatcat
    Dec 1 '18 at 3:45



















I am a bit curious - do you need loops (apply) for some reason?

– jezrael
Nov 29 '18 at 6:59





I am a bit curious - do you need loops (apply) for some reason?

– jezrael
Nov 29 '18 at 6:59













Nope, if there's a better syntax i'm happy to use it. I also just went through your answer, and understood it now. Thank you.

– abigfatcat
Dec 1 '18 at 3:45







Nope, if there's a better syntax i'm happy to use it. I also just went through your answer, and understood it now. Thank you.

– abigfatcat
Dec 1 '18 at 3:45














2 Answers
2






active

oldest

votes


















1














In pandas is best avoid loops (apply are loops under the hood) if exist vectorized solutions, because loops are slow.



I try rewrite your code - create dictionary with output and list of values, swap keys with values and call map, last for not matched values add fillna:



d = {'Residential' :['Multifamily Housing', 'Residence Hall/Dormitory'],
'Commercial' : ['Bank Branch', 'Hotel' , 'Financial Office' , 'Retail Store', 'Distribution Center',
'Non-Refrigerated Warehouse', 'Fitness Center/Health Club/Gym', 'Mixed Use Property',
'Self-Storage Facility', 'Wholesale Club/Supercenter', 'Supermarket/Grocery Store'],
'Institutional':['Senior Care Community', 'K-12 School', 'College/University', 'Worship Facility',
'Medical Office', 'Hospital (General Medical & Surgical)'],
'Industrial': ['Manufacturing/Industrial Plant'] }




d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}
print (d1)

{
'Multifamily Housing': 'Residential',
'Residence Hall/Dormitory': 'Residential',
'Bank Branch': 'Commercial',
'Hotel': 'Commercial',
'Financial Office': 'Commercial',
'Retail Store': 'Commercial',
'Distribution Center': 'Commercial',
'Non-Refrigerated Warehouse': 'Commercial',
'Fitness Center/Health Club/Gym': 'Commercial',
'Mixed Use Property': 'Commercial',
'Self-Storage Facility': 'Commercial',
'Wholesale Club/Supercenter': 'Commercial',
'Supermarket/Grocery Store': 'Commercial',
'Senior Care Community': 'Institutional',
'K-12 School': 'Institutional',
'College/University': 'Institutional',
'Worship Facility': 'Institutional',
'Medical Office': 'Institutional',
'Hospital (General Medical & Surgical)': 'Institutional',
'Manufacturing/Industrial Plant': 'Industrial'
}




datav3 = pd.DataFrame({'Program':['Medical Office','Hotel',
'Residence Hall/Dormitory',
'Manufacturing/Industrial Plant','House']})
datav3['Program Type'] = datav3['Program'].map(d1).fillna('Other')
print (datav3)
Program Program Type
0 Medical Office Institutional
1 Hotel Commercial
2 Residence Hall/Dormitory Residential
3 Manufacturing/Industrial Plant Industrial
4 House Other





share|improve this answer


























  • Could you please explain a little on this bit if you don't mind, i'm new to this syntax: d1 = {k: oldk for oldk, oldv in d.items() for k in oldv} Thanks!

    – abigfatcat
    Dec 1 '18 at 6:08













  • @SchratchieMe You can check this

    – jezrael
    Dec 1 '18 at 9:47











  • @SchratchieMe and this

    – jezrael
    Dec 1 '18 at 9:49



















1














The issue is with your if loops. The way you are comparing after or is not correct.



Writing or 'Residence Hall/Dormitory' will always be true, hence, only the first if gets evaluated everytime and you get Residential in all rows.



Instead of this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':


Do this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory':


OR



if any([c['Primary Property Type - Self Selected'] == 'Multifamily Housing', c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory']):


Just make the above change, and your code should do what is expected. Hope this is clear.






share|improve this answer


























  • @ScratchieMe Please consider upvoting the answer too. Thanks

    – Mayank Porwal
    Nov 29 '18 at 7:12











  • I did so, thanks very much

    – abigfatcat
    Dec 1 '18 at 3:46












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%2f53533171%2fcreate-new-column-in-pandas-df-and-assign-string-values-conditionally%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









1














In pandas is best avoid loops (apply are loops under the hood) if exist vectorized solutions, because loops are slow.



I try rewrite your code - create dictionary with output and list of values, swap keys with values and call map, last for not matched values add fillna:



d = {'Residential' :['Multifamily Housing', 'Residence Hall/Dormitory'],
'Commercial' : ['Bank Branch', 'Hotel' , 'Financial Office' , 'Retail Store', 'Distribution Center',
'Non-Refrigerated Warehouse', 'Fitness Center/Health Club/Gym', 'Mixed Use Property',
'Self-Storage Facility', 'Wholesale Club/Supercenter', 'Supermarket/Grocery Store'],
'Institutional':['Senior Care Community', 'K-12 School', 'College/University', 'Worship Facility',
'Medical Office', 'Hospital (General Medical & Surgical)'],
'Industrial': ['Manufacturing/Industrial Plant'] }




d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}
print (d1)

{
'Multifamily Housing': 'Residential',
'Residence Hall/Dormitory': 'Residential',
'Bank Branch': 'Commercial',
'Hotel': 'Commercial',
'Financial Office': 'Commercial',
'Retail Store': 'Commercial',
'Distribution Center': 'Commercial',
'Non-Refrigerated Warehouse': 'Commercial',
'Fitness Center/Health Club/Gym': 'Commercial',
'Mixed Use Property': 'Commercial',
'Self-Storage Facility': 'Commercial',
'Wholesale Club/Supercenter': 'Commercial',
'Supermarket/Grocery Store': 'Commercial',
'Senior Care Community': 'Institutional',
'K-12 School': 'Institutional',
'College/University': 'Institutional',
'Worship Facility': 'Institutional',
'Medical Office': 'Institutional',
'Hospital (General Medical & Surgical)': 'Institutional',
'Manufacturing/Industrial Plant': 'Industrial'
}




datav3 = pd.DataFrame({'Program':['Medical Office','Hotel',
'Residence Hall/Dormitory',
'Manufacturing/Industrial Plant','House']})
datav3['Program Type'] = datav3['Program'].map(d1).fillna('Other')
print (datav3)
Program Program Type
0 Medical Office Institutional
1 Hotel Commercial
2 Residence Hall/Dormitory Residential
3 Manufacturing/Industrial Plant Industrial
4 House Other





share|improve this answer


























  • Could you please explain a little on this bit if you don't mind, i'm new to this syntax: d1 = {k: oldk for oldk, oldv in d.items() for k in oldv} Thanks!

    – abigfatcat
    Dec 1 '18 at 6:08













  • @SchratchieMe You can check this

    – jezrael
    Dec 1 '18 at 9:47











  • @SchratchieMe and this

    – jezrael
    Dec 1 '18 at 9:49
















1














In pandas is best avoid loops (apply are loops under the hood) if exist vectorized solutions, because loops are slow.



I try rewrite your code - create dictionary with output and list of values, swap keys with values and call map, last for not matched values add fillna:



d = {'Residential' :['Multifamily Housing', 'Residence Hall/Dormitory'],
'Commercial' : ['Bank Branch', 'Hotel' , 'Financial Office' , 'Retail Store', 'Distribution Center',
'Non-Refrigerated Warehouse', 'Fitness Center/Health Club/Gym', 'Mixed Use Property',
'Self-Storage Facility', 'Wholesale Club/Supercenter', 'Supermarket/Grocery Store'],
'Institutional':['Senior Care Community', 'K-12 School', 'College/University', 'Worship Facility',
'Medical Office', 'Hospital (General Medical & Surgical)'],
'Industrial': ['Manufacturing/Industrial Plant'] }




d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}
print (d1)

{
'Multifamily Housing': 'Residential',
'Residence Hall/Dormitory': 'Residential',
'Bank Branch': 'Commercial',
'Hotel': 'Commercial',
'Financial Office': 'Commercial',
'Retail Store': 'Commercial',
'Distribution Center': 'Commercial',
'Non-Refrigerated Warehouse': 'Commercial',
'Fitness Center/Health Club/Gym': 'Commercial',
'Mixed Use Property': 'Commercial',
'Self-Storage Facility': 'Commercial',
'Wholesale Club/Supercenter': 'Commercial',
'Supermarket/Grocery Store': 'Commercial',
'Senior Care Community': 'Institutional',
'K-12 School': 'Institutional',
'College/University': 'Institutional',
'Worship Facility': 'Institutional',
'Medical Office': 'Institutional',
'Hospital (General Medical & Surgical)': 'Institutional',
'Manufacturing/Industrial Plant': 'Industrial'
}




datav3 = pd.DataFrame({'Program':['Medical Office','Hotel',
'Residence Hall/Dormitory',
'Manufacturing/Industrial Plant','House']})
datav3['Program Type'] = datav3['Program'].map(d1).fillna('Other')
print (datav3)
Program Program Type
0 Medical Office Institutional
1 Hotel Commercial
2 Residence Hall/Dormitory Residential
3 Manufacturing/Industrial Plant Industrial
4 House Other





share|improve this answer


























  • Could you please explain a little on this bit if you don't mind, i'm new to this syntax: d1 = {k: oldk for oldk, oldv in d.items() for k in oldv} Thanks!

    – abigfatcat
    Dec 1 '18 at 6:08













  • @SchratchieMe You can check this

    – jezrael
    Dec 1 '18 at 9:47











  • @SchratchieMe and this

    – jezrael
    Dec 1 '18 at 9:49














1












1








1







In pandas is best avoid loops (apply are loops under the hood) if exist vectorized solutions, because loops are slow.



I try rewrite your code - create dictionary with output and list of values, swap keys with values and call map, last for not matched values add fillna:



d = {'Residential' :['Multifamily Housing', 'Residence Hall/Dormitory'],
'Commercial' : ['Bank Branch', 'Hotel' , 'Financial Office' , 'Retail Store', 'Distribution Center',
'Non-Refrigerated Warehouse', 'Fitness Center/Health Club/Gym', 'Mixed Use Property',
'Self-Storage Facility', 'Wholesale Club/Supercenter', 'Supermarket/Grocery Store'],
'Institutional':['Senior Care Community', 'K-12 School', 'College/University', 'Worship Facility',
'Medical Office', 'Hospital (General Medical & Surgical)'],
'Industrial': ['Manufacturing/Industrial Plant'] }




d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}
print (d1)

{
'Multifamily Housing': 'Residential',
'Residence Hall/Dormitory': 'Residential',
'Bank Branch': 'Commercial',
'Hotel': 'Commercial',
'Financial Office': 'Commercial',
'Retail Store': 'Commercial',
'Distribution Center': 'Commercial',
'Non-Refrigerated Warehouse': 'Commercial',
'Fitness Center/Health Club/Gym': 'Commercial',
'Mixed Use Property': 'Commercial',
'Self-Storage Facility': 'Commercial',
'Wholesale Club/Supercenter': 'Commercial',
'Supermarket/Grocery Store': 'Commercial',
'Senior Care Community': 'Institutional',
'K-12 School': 'Institutional',
'College/University': 'Institutional',
'Worship Facility': 'Institutional',
'Medical Office': 'Institutional',
'Hospital (General Medical & Surgical)': 'Institutional',
'Manufacturing/Industrial Plant': 'Industrial'
}




datav3 = pd.DataFrame({'Program':['Medical Office','Hotel',
'Residence Hall/Dormitory',
'Manufacturing/Industrial Plant','House']})
datav3['Program Type'] = datav3['Program'].map(d1).fillna('Other')
print (datav3)
Program Program Type
0 Medical Office Institutional
1 Hotel Commercial
2 Residence Hall/Dormitory Residential
3 Manufacturing/Industrial Plant Industrial
4 House Other





share|improve this answer















In pandas is best avoid loops (apply are loops under the hood) if exist vectorized solutions, because loops are slow.



I try rewrite your code - create dictionary with output and list of values, swap keys with values and call map, last for not matched values add fillna:



d = {'Residential' :['Multifamily Housing', 'Residence Hall/Dormitory'],
'Commercial' : ['Bank Branch', 'Hotel' , 'Financial Office' , 'Retail Store', 'Distribution Center',
'Non-Refrigerated Warehouse', 'Fitness Center/Health Club/Gym', 'Mixed Use Property',
'Self-Storage Facility', 'Wholesale Club/Supercenter', 'Supermarket/Grocery Store'],
'Institutional':['Senior Care Community', 'K-12 School', 'College/University', 'Worship Facility',
'Medical Office', 'Hospital (General Medical & Surgical)'],
'Industrial': ['Manufacturing/Industrial Plant'] }




d1 = {k: oldk for oldk, oldv in d.items() for k in oldv}
print (d1)

{
'Multifamily Housing': 'Residential',
'Residence Hall/Dormitory': 'Residential',
'Bank Branch': 'Commercial',
'Hotel': 'Commercial',
'Financial Office': 'Commercial',
'Retail Store': 'Commercial',
'Distribution Center': 'Commercial',
'Non-Refrigerated Warehouse': 'Commercial',
'Fitness Center/Health Club/Gym': 'Commercial',
'Mixed Use Property': 'Commercial',
'Self-Storage Facility': 'Commercial',
'Wholesale Club/Supercenter': 'Commercial',
'Supermarket/Grocery Store': 'Commercial',
'Senior Care Community': 'Institutional',
'K-12 School': 'Institutional',
'College/University': 'Institutional',
'Worship Facility': 'Institutional',
'Medical Office': 'Institutional',
'Hospital (General Medical & Surgical)': 'Institutional',
'Manufacturing/Industrial Plant': 'Industrial'
}




datav3 = pd.DataFrame({'Program':['Medical Office','Hotel',
'Residence Hall/Dormitory',
'Manufacturing/Industrial Plant','House']})
datav3['Program Type'] = datav3['Program'].map(d1).fillna('Other')
print (datav3)
Program Program Type
0 Medical Office Institutional
1 Hotel Commercial
2 Residence Hall/Dormitory Residential
3 Manufacturing/Industrial Plant Industrial
4 House Other






share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 6:53

























answered Nov 29 '18 at 6:42









jezraeljezrael

357k26321397




357k26321397













  • Could you please explain a little on this bit if you don't mind, i'm new to this syntax: d1 = {k: oldk for oldk, oldv in d.items() for k in oldv} Thanks!

    – abigfatcat
    Dec 1 '18 at 6:08













  • @SchratchieMe You can check this

    – jezrael
    Dec 1 '18 at 9:47











  • @SchratchieMe and this

    – jezrael
    Dec 1 '18 at 9:49



















  • Could you please explain a little on this bit if you don't mind, i'm new to this syntax: d1 = {k: oldk for oldk, oldv in d.items() for k in oldv} Thanks!

    – abigfatcat
    Dec 1 '18 at 6:08













  • @SchratchieMe You can check this

    – jezrael
    Dec 1 '18 at 9:47











  • @SchratchieMe and this

    – jezrael
    Dec 1 '18 at 9:49

















Could you please explain a little on this bit if you don't mind, i'm new to this syntax: d1 = {k: oldk for oldk, oldv in d.items() for k in oldv} Thanks!

– abigfatcat
Dec 1 '18 at 6:08







Could you please explain a little on this bit if you don't mind, i'm new to this syntax: d1 = {k: oldk for oldk, oldv in d.items() for k in oldv} Thanks!

– abigfatcat
Dec 1 '18 at 6:08















@SchratchieMe You can check this

– jezrael
Dec 1 '18 at 9:47





@SchratchieMe You can check this

– jezrael
Dec 1 '18 at 9:47













@SchratchieMe and this

– jezrael
Dec 1 '18 at 9:49





@SchratchieMe and this

– jezrael
Dec 1 '18 at 9:49













1














The issue is with your if loops. The way you are comparing after or is not correct.



Writing or 'Residence Hall/Dormitory' will always be true, hence, only the first if gets evaluated everytime and you get Residential in all rows.



Instead of this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':


Do this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory':


OR



if any([c['Primary Property Type - Self Selected'] == 'Multifamily Housing', c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory']):


Just make the above change, and your code should do what is expected. Hope this is clear.






share|improve this answer


























  • @ScratchieMe Please consider upvoting the answer too. Thanks

    – Mayank Porwal
    Nov 29 '18 at 7:12











  • I did so, thanks very much

    – abigfatcat
    Dec 1 '18 at 3:46
















1














The issue is with your if loops. The way you are comparing after or is not correct.



Writing or 'Residence Hall/Dormitory' will always be true, hence, only the first if gets evaluated everytime and you get Residential in all rows.



Instead of this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':


Do this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory':


OR



if any([c['Primary Property Type - Self Selected'] == 'Multifamily Housing', c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory']):


Just make the above change, and your code should do what is expected. Hope this is clear.






share|improve this answer


























  • @ScratchieMe Please consider upvoting the answer too. Thanks

    – Mayank Porwal
    Nov 29 '18 at 7:12











  • I did so, thanks very much

    – abigfatcat
    Dec 1 '18 at 3:46














1












1








1







The issue is with your if loops. The way you are comparing after or is not correct.



Writing or 'Residence Hall/Dormitory' will always be true, hence, only the first if gets evaluated everytime and you get Residential in all rows.



Instead of this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':


Do this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory':


OR



if any([c['Primary Property Type - Self Selected'] == 'Multifamily Housing', c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory']):


Just make the above change, and your code should do what is expected. Hope this is clear.






share|improve this answer















The issue is with your if loops. The way you are comparing after or is not correct.



Writing or 'Residence Hall/Dormitory' will always be true, hence, only the first if gets evaluated everytime and you get Residential in all rows.



Instead of this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or 'Residence Hall/Dormitory':


Do this:



if c['Primary Property Type - Self Selected'] == 'Multifamily Housing' or c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory':


OR



if any([c['Primary Property Type - Self Selected'] == 'Multifamily Housing', c['Primary Property Type - Self Selected'] == 'Residence Hall/Dormitory']):


Just make the above change, and your code should do what is expected. Hope this is clear.







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 29 '18 at 6:52

























answered Nov 29 '18 at 6:42









Mayank PorwalMayank Porwal

5,0182725




5,0182725













  • @ScratchieMe Please consider upvoting the answer too. Thanks

    – Mayank Porwal
    Nov 29 '18 at 7:12











  • I did so, thanks very much

    – abigfatcat
    Dec 1 '18 at 3:46



















  • @ScratchieMe Please consider upvoting the answer too. Thanks

    – Mayank Porwal
    Nov 29 '18 at 7:12











  • I did so, thanks very much

    – abigfatcat
    Dec 1 '18 at 3:46

















@ScratchieMe Please consider upvoting the answer too. Thanks

– Mayank Porwal
Nov 29 '18 at 7:12





@ScratchieMe Please consider upvoting the answer too. Thanks

– Mayank Porwal
Nov 29 '18 at 7:12













I did so, thanks very much

– abigfatcat
Dec 1 '18 at 3:46





I did so, thanks very much

– abigfatcat
Dec 1 '18 at 3:46


















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%2f53533171%2fcreate-new-column-in-pandas-df-and-assign-string-values-conditionally%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

Contact image not getting when fetch all contact list from iPhone by CNContact

Idjassú

count number of partitions of a set with n elements into k subsets