How to change a value in a column based on whether or not a certain string combination is in other columns in...











up vote
1
down vote

favorite












I am a very new newbie to Pandas and programming in general. I'm using Anaconda, if that matters.



I have the following on my hands:





The infamous Titanic survival dataset.



So, my idea was to search the dataframe, find the rows where in the "Name" column there would be a string "Mrs." AND at the same time the "age" would be a NaN (in which case the value in the "Age" column needs to be changed to 32). Also, finding "Miss"in the cell, values in two other columns are zeros.



My major problem is that I don't know how to tell Pandas to replace the value in the same row or delete the whole row.



    #I decided to collect the indexes of rows with the "Age" value == NaN to further use the
#indices to search through the "Names column."

list_of_NaNs = df[df['Age'].isnull()].index.tolist()

for name in df.Name:
if "Mrs." in name and name (list_of_NaNs):#if the string combination "Mrs."
#can be found within the cell...
df.loc['Age'] = 32.5 #need to change the value in the
#column IN THE SAME ROW
elif "Miss" in name and df.loc[Parch]>0: #how to make a
#reference to a value IN THE SAME ROW???
df.loc["Age"] = 5
elif df.SibSp ==0 and Parch ==0:
df.loc["Age"] = 32.5
else:
#mmm... how do I delete entire row so that it doesn't
#interfere with my future actions?









share|improve this question




























    up vote
    1
    down vote

    favorite












    I am a very new newbie to Pandas and programming in general. I'm using Anaconda, if that matters.



    I have the following on my hands:





    The infamous Titanic survival dataset.



    So, my idea was to search the dataframe, find the rows where in the "Name" column there would be a string "Mrs." AND at the same time the "age" would be a NaN (in which case the value in the "Age" column needs to be changed to 32). Also, finding "Miss"in the cell, values in two other columns are zeros.



    My major problem is that I don't know how to tell Pandas to replace the value in the same row or delete the whole row.



        #I decided to collect the indexes of rows with the "Age" value == NaN to further use the
    #indices to search through the "Names column."

    list_of_NaNs = df[df['Age'].isnull()].index.tolist()

    for name in df.Name:
    if "Mrs." in name and name (list_of_NaNs):#if the string combination "Mrs."
    #can be found within the cell...
    df.loc['Age'] = 32.5 #need to change the value in the
    #column IN THE SAME ROW
    elif "Miss" in name and df.loc[Parch]>0: #how to make a
    #reference to a value IN THE SAME ROW???
    df.loc["Age"] = 5
    elif df.SibSp ==0 and Parch ==0:
    df.loc["Age"] = 32.5
    else:
    #mmm... how do I delete entire row so that it doesn't
    #interfere with my future actions?









    share|improve this question


























      up vote
      1
      down vote

      favorite









      up vote
      1
      down vote

      favorite











      I am a very new newbie to Pandas and programming in general. I'm using Anaconda, if that matters.



      I have the following on my hands:





      The infamous Titanic survival dataset.



      So, my idea was to search the dataframe, find the rows where in the "Name" column there would be a string "Mrs." AND at the same time the "age" would be a NaN (in which case the value in the "Age" column needs to be changed to 32). Also, finding "Miss"in the cell, values in two other columns are zeros.



      My major problem is that I don't know how to tell Pandas to replace the value in the same row or delete the whole row.



          #I decided to collect the indexes of rows with the "Age" value == NaN to further use the
      #indices to search through the "Names column."

      list_of_NaNs = df[df['Age'].isnull()].index.tolist()

      for name in df.Name:
      if "Mrs." in name and name (list_of_NaNs):#if the string combination "Mrs."
      #can be found within the cell...
      df.loc['Age'] = 32.5 #need to change the value in the
      #column IN THE SAME ROW
      elif "Miss" in name and df.loc[Parch]>0: #how to make a
      #reference to a value IN THE SAME ROW???
      df.loc["Age"] = 5
      elif df.SibSp ==0 and Parch ==0:
      df.loc["Age"] = 32.5
      else:
      #mmm... how do I delete entire row so that it doesn't
      #interfere with my future actions?









      share|improve this question















      I am a very new newbie to Pandas and programming in general. I'm using Anaconda, if that matters.



      I have the following on my hands:





      The infamous Titanic survival dataset.



      So, my idea was to search the dataframe, find the rows where in the "Name" column there would be a string "Mrs." AND at the same time the "age" would be a NaN (in which case the value in the "Age" column needs to be changed to 32). Also, finding "Miss"in the cell, values in two other columns are zeros.



      My major problem is that I don't know how to tell Pandas to replace the value in the same row or delete the whole row.



          #I decided to collect the indexes of rows with the "Age" value == NaN to further use the
      #indices to search through the "Names column."

      list_of_NaNs = df[df['Age'].isnull()].index.tolist()

      for name in df.Name:
      if "Mrs." in name and name (list_of_NaNs):#if the string combination "Mrs."
      #can be found within the cell...
      df.loc['Age'] = 32.5 #need to change the value in the
      #column IN THE SAME ROW
      elif "Miss" in name and df.loc[Parch]>0: #how to make a
      #reference to a value IN THE SAME ROW???
      df.loc["Age"] = 5
      elif df.SibSp ==0 and Parch ==0:
      df.loc["Age"] = 32.5
      else:
      #mmm... how do I delete entire row so that it doesn't
      #interfere with my future actions?






      pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 10 at 5:45









      Foo

      1




      1










      asked Nov 10 at 5:20









      Olga

      154




      154
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          Here is how you can test if 'Miss' or 'Mrs.'is present in name columns:



          df.name.str.contains('Mrs')


          So following will give you the rows where 'Mrs' is in name and Age is NaN



          df[(df.name.str.contains('Mrs')) & (df.age.isna())]


          You can play with different cases and tasks from here on.



          Hope this helps :)



          And to drop rows with NaN in age column:



          df = df.drop(df[df.age.isna()].index)





          share|improve this answer























          • Why, yes, this is immense help! Thank you so much!
            – Olga
            Nov 10 at 7:43










          • If it's not too much trouble, could you also show how to delete the row with an age NaN..?
            – Olga
            Nov 10 at 7:44










          • Cool, just added the code to delete rows in the last line of answer above. Please uptick and accept as final answer :)
            – Pankaj Joshi
            Nov 10 at 8:29











          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',
          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%2f53236207%2fhow-to-change-a-value-in-a-column-based-on-whether-or-not-a-certain-string-combi%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








          up vote
          0
          down vote



          accepted










          Here is how you can test if 'Miss' or 'Mrs.'is present in name columns:



          df.name.str.contains('Mrs')


          So following will give you the rows where 'Mrs' is in name and Age is NaN



          df[(df.name.str.contains('Mrs')) & (df.age.isna())]


          You can play with different cases and tasks from here on.



          Hope this helps :)



          And to drop rows with NaN in age column:



          df = df.drop(df[df.age.isna()].index)





          share|improve this answer























          • Why, yes, this is immense help! Thank you so much!
            – Olga
            Nov 10 at 7:43










          • If it's not too much trouble, could you also show how to delete the row with an age NaN..?
            – Olga
            Nov 10 at 7:44










          • Cool, just added the code to delete rows in the last line of answer above. Please uptick and accept as final answer :)
            – Pankaj Joshi
            Nov 10 at 8:29















          up vote
          0
          down vote



          accepted










          Here is how you can test if 'Miss' or 'Mrs.'is present in name columns:



          df.name.str.contains('Mrs')


          So following will give you the rows where 'Mrs' is in name and Age is NaN



          df[(df.name.str.contains('Mrs')) & (df.age.isna())]


          You can play with different cases and tasks from here on.



          Hope this helps :)



          And to drop rows with NaN in age column:



          df = df.drop(df[df.age.isna()].index)





          share|improve this answer























          • Why, yes, this is immense help! Thank you so much!
            – Olga
            Nov 10 at 7:43










          • If it's not too much trouble, could you also show how to delete the row with an age NaN..?
            – Olga
            Nov 10 at 7:44










          • Cool, just added the code to delete rows in the last line of answer above. Please uptick and accept as final answer :)
            – Pankaj Joshi
            Nov 10 at 8:29













          up vote
          0
          down vote



          accepted







          up vote
          0
          down vote



          accepted






          Here is how you can test if 'Miss' or 'Mrs.'is present in name columns:



          df.name.str.contains('Mrs')


          So following will give you the rows where 'Mrs' is in name and Age is NaN



          df[(df.name.str.contains('Mrs')) & (df.age.isna())]


          You can play with different cases and tasks from here on.



          Hope this helps :)



          And to drop rows with NaN in age column:



          df = df.drop(df[df.age.isna()].index)





          share|improve this answer














          Here is how you can test if 'Miss' or 'Mrs.'is present in name columns:



          df.name.str.contains('Mrs')


          So following will give you the rows where 'Mrs' is in name and Age is NaN



          df[(df.name.str.contains('Mrs')) & (df.age.isna())]


          You can play with different cases and tasks from here on.



          Hope this helps :)



          And to drop rows with NaN in age column:



          df = df.drop(df[df.age.isna()].index)






          share|improve this answer














          share|improve this answer



          share|improve this answer








          edited Nov 10 at 8:28

























          answered Nov 10 at 5:38









          Pankaj Joshi

          911310




          911310












          • Why, yes, this is immense help! Thank you so much!
            – Olga
            Nov 10 at 7:43










          • If it's not too much trouble, could you also show how to delete the row with an age NaN..?
            – Olga
            Nov 10 at 7:44










          • Cool, just added the code to delete rows in the last line of answer above. Please uptick and accept as final answer :)
            – Pankaj Joshi
            Nov 10 at 8:29


















          • Why, yes, this is immense help! Thank you so much!
            – Olga
            Nov 10 at 7:43










          • If it's not too much trouble, could you also show how to delete the row with an age NaN..?
            – Olga
            Nov 10 at 7:44










          • Cool, just added the code to delete rows in the last line of answer above. Please uptick and accept as final answer :)
            – Pankaj Joshi
            Nov 10 at 8:29
















          Why, yes, this is immense help! Thank you so much!
          – Olga
          Nov 10 at 7:43




          Why, yes, this is immense help! Thank you so much!
          – Olga
          Nov 10 at 7:43












          If it's not too much trouble, could you also show how to delete the row with an age NaN..?
          – Olga
          Nov 10 at 7:44




          If it's not too much trouble, could you also show how to delete the row with an age NaN..?
          – Olga
          Nov 10 at 7:44












          Cool, just added the code to delete rows in the last line of answer above. Please uptick and accept as final answer :)
          – Pankaj Joshi
          Nov 10 at 8:29




          Cool, just added the code to delete rows in the last line of answer above. Please uptick and accept as final answer :)
          – Pankaj Joshi
          Nov 10 at 8:29


















          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.





          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.




          draft saved


          draft discarded














          StackExchange.ready(
          function () {
          StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53236207%2fhow-to-change-a-value-in-a-column-based-on-whether-or-not-a-certain-string-combi%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

          Schultheiß

          Verwaltungsgliederung Dänemarks

          Liste der Kulturdenkmale in Wilsdruff