How to use one column to cover another column and adopt another column value when NaN?How do I sort a dictionary by value?Add one row to pandas DataFrameAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeCreating an empty Pandas DataFrame, then filling it?Set value for particular cell in pandas DataFrame using index“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Hostile Divisor Numbers

How to properly store the current value of int variable into a token list?

Page count conversion from single to double-space for submissions

What happens if I accidentally leave an app running and click "Install Now" in Software Updater?

What do you call a painting on a wall?

What is the closest airport to the center of the city it serves?

It isn’t that you must stop now

Why are oscilloscope input impedances so low?

Why is "breaking the mould" positively connoted?

Sheared off exhasut pipe: How to fix without a welder?

Why is my arithmetic with a long long int behaving this way?

As a GM, is it bad form to ask for a moment to think when improvising?

Is 'contemporary' ambiguous and if so is there a better word?

How to calculate rate of axial precession?

My large rocket is still flipping over

Has the Hulk always been able to talk?

Dangerous workplace travelling

How to pass query parameters in URL in Salesforce Summer 19 Release?

How to remap repeating commands i.e. <number><command>?

Is disk brake effectiveness mitigated by tyres losing traction under strong braking?

Dirichlet series with a single zero

How to preserve a rare version of a book?

Simple Derivative Proof?

Would a "Permanence" spell in 5e be overpowered?



How to use one column to cover another column and adopt another column value when NaN?


How do I sort a dictionary by value?Add one row to pandas DataFrameAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeCreating an empty Pandas DataFrame, then filling it?Set value for particular cell in pandas DataFrame using index“Large data” work flows using pandasHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers






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








2















My system is Python3.6 with pandas 0.24



There are 2 column cola and colb in my dataframe dfas below:



cola colb
A C
C NaN
NaN C
C D


I want to use colb to cover cola if both has row value,adopt cola if colb has no value.



The expected result as below:



cola colb colc
A C C
C NaN C
NaN C C
C D D


How to do it?

Thanks in advance!










share|improve this question






























    2















    My system is Python3.6 with pandas 0.24



    There are 2 column cola and colb in my dataframe dfas below:



    cola colb
    A C
    C NaN
    NaN C
    C D


    I want to use colb to cover cola if both has row value,adopt cola if colb has no value.



    The expected result as below:



    cola colb colc
    A C C
    C NaN C
    NaN C C
    C D D


    How to do it?

    Thanks in advance!










    share|improve this question


























      2












      2








      2








      My system is Python3.6 with pandas 0.24



      There are 2 column cola and colb in my dataframe dfas below:



      cola colb
      A C
      C NaN
      NaN C
      C D


      I want to use colb to cover cola if both has row value,adopt cola if colb has no value.



      The expected result as below:



      cola colb colc
      A C C
      C NaN C
      NaN C C
      C D D


      How to do it?

      Thanks in advance!










      share|improve this question
















      My system is Python3.6 with pandas 0.24



      There are 2 column cola and colb in my dataframe dfas below:



      cola colb
      A C
      C NaN
      NaN C
      C D


      I want to use colb to cover cola if both has row value,adopt cola if colb has no value.



      The expected result as below:



      cola colb colc
      A C C
      C NaN C
      NaN C C
      C D D


      How to do it?

      Thanks in advance!







      python pandas dataframe






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Apr 3 at 13:26









      Scott Boston

      60k73258




      60k73258










      asked Mar 23 at 3:07









      kittygirlkittygirl

      590617




      590617






















          2 Answers
          2






          active

          oldest

          votes


















          1














          This is more like fillna with pd.Series



          df.colb.fillna(df.cola)
          Out[593]:
          0 C
          1 C
          2 C
          3 D
          Name: colb, dtype: object
          #df['colc']=df.colb.fillna(df.cola)





          share|improve this answer






























            2














            You can use np.where



            df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])

            print(df)


            Output:



             cola colb colc
            0 A C C
            1 C None C
            2 None C C
            3 C D D





            share|improve this answer























              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%2f55310233%2fhow-to-use-one-column-to-cover-another-column-and-adopt-another-column-value-whe%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














              This is more like fillna with pd.Series



              df.colb.fillna(df.cola)
              Out[593]:
              0 C
              1 C
              2 C
              3 D
              Name: colb, dtype: object
              #df['colc']=df.colb.fillna(df.cola)





              share|improve this answer



























                1














                This is more like fillna with pd.Series



                df.colb.fillna(df.cola)
                Out[593]:
                0 C
                1 C
                2 C
                3 D
                Name: colb, dtype: object
                #df['colc']=df.colb.fillna(df.cola)





                share|improve this answer

























                  1












                  1








                  1







                  This is more like fillna with pd.Series



                  df.colb.fillna(df.cola)
                  Out[593]:
                  0 C
                  1 C
                  2 C
                  3 D
                  Name: colb, dtype: object
                  #df['colc']=df.colb.fillna(df.cola)





                  share|improve this answer













                  This is more like fillna with pd.Series



                  df.colb.fillna(df.cola)
                  Out[593]:
                  0 C
                  1 C
                  2 C
                  3 D
                  Name: colb, dtype: object
                  #df['colc']=df.colb.fillna(df.cola)






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 23 at 3:48









                  Wen-BenWen-Ben

                  131k83972




                  131k83972























                      2














                      You can use np.where



                      df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])

                      print(df)


                      Output:



                       cola colb colc
                      0 A C C
                      1 C None C
                      2 None C C
                      3 C D D





                      share|improve this answer



























                        2














                        You can use np.where



                        df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])

                        print(df)


                        Output:



                         cola colb colc
                        0 A C C
                        1 C None C
                        2 None C C
                        3 C D D





                        share|improve this answer

























                          2












                          2








                          2







                          You can use np.where



                          df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])

                          print(df)


                          Output:



                           cola colb colc
                          0 A C C
                          1 C None C
                          2 None C C
                          3 C D D





                          share|improve this answer













                          You can use np.where



                          df['colc']=np.where(df['colb'].isnull(), df['cola'], df['colb'])

                          print(df)


                          Output:



                           cola colb colc
                          0 A C C
                          1 C None C
                          2 None C C
                          3 C D D






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 23 at 4:05









                          AkshayNevrekarAkshayNevrekar

                          6,419102243




                          6,419102243



























                              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%2f55310233%2fhow-to-use-one-column-to-cover-another-column-and-adopt-another-column-value-whe%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

                              Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

                              Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

                              Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript