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

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현