How to fill missing value based on other columns in Pandas dataframe?












8












$begingroup$


Suppose I have a 5*3 data frame in which third column contains missing value



1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN


I hope to generate value for missing value based rule that first product second column



1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6


How can I do it use data frame? Thanks.



How to add condition to calculate missing value like this?



if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd



1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1









share|improve this question











$endgroup$












  • $begingroup$
    You can't do this because the size will not be equal
    $endgroup$
    – Mayur Dangar
    Jul 7 '18 at 5:53










  • $begingroup$
    Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
    $endgroup$
    – Damian Melniczuk
    Jul 7 '18 at 6:25
















8












$begingroup$


Suppose I have a 5*3 data frame in which third column contains missing value



1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN


I hope to generate value for missing value based rule that first product second column



1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6


How can I do it use data frame? Thanks.



How to add condition to calculate missing value like this?



if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd



1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1









share|improve this question











$endgroup$












  • $begingroup$
    You can't do this because the size will not be equal
    $endgroup$
    – Mayur Dangar
    Jul 7 '18 at 5:53










  • $begingroup$
    Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
    $endgroup$
    – Damian Melniczuk
    Jul 7 '18 at 6:25














8












8








8


3



$begingroup$


Suppose I have a 5*3 data frame in which third column contains missing value



1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN


I hope to generate value for missing value based rule that first product second column



1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6


How can I do it use data frame? Thanks.



How to add condition to calculate missing value like this?



if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd



1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1









share|improve this question











$endgroup$




Suppose I have a 5*3 data frame in which third column contains missing value



1 2 3
4 5 NaN
7 8 9
3 2 NaN
5 6 NaN


I hope to generate value for missing value based rule that first product second column



1 2 3
4 5 20 <--4*5
7 8 9
3 2 6 <-- 3*2
5 6 30 <-- 5*6


How can I do it use data frame? Thanks.



How to add condition to calculate missing value like this?



if 1st % 2 == 0 then 3rd = 1st * 2nd
else 3rd = 1st + 2nd



1 2 3
4 5 20 <-- 4*5 because 4%2==0
7 8 9
3 2 5 <-- 3+2 because 3%2==1
5 6 11 <-- 5+6 because 5%2==1






pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 22 '17 at 13:10







KyL

















asked Mar 22 '17 at 12:57









KyLKyL

149114




149114












  • $begingroup$
    You can't do this because the size will not be equal
    $endgroup$
    – Mayur Dangar
    Jul 7 '18 at 5:53










  • $begingroup$
    Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
    $endgroup$
    – Damian Melniczuk
    Jul 7 '18 at 6:25


















  • $begingroup$
    You can't do this because the size will not be equal
    $endgroup$
    – Mayur Dangar
    Jul 7 '18 at 5:53










  • $begingroup$
    Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
    $endgroup$
    – Damian Melniczuk
    Jul 7 '18 at 6:25
















$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53




$begingroup$
You can't do this because the size will not be equal
$endgroup$
– Mayur Dangar
Jul 7 '18 at 5:53












$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25




$begingroup$
Can you expand your answer? Why isn't it possible and what could he possibly do to solve problem?
$endgroup$
– Damian Melniczuk
Jul 7 '18 at 6:25










4 Answers
4






active

oldest

votes


















10












$begingroup$

Assuming three columns of your dataframe is a, b and c. This is what you want:



df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)


Full code:



df = pd.DataFrame(
np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
columns=['a', 'b', 'c']
)
df['c'] = df.apply(
lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
axis=1
)





share|improve this answer









$endgroup$





















    2












    $begingroup$

    Assuming that the three columns in your dataframe are a, b and c. Then you can do the required operation like this:



    values = df['a'] * df['b']
    df['c'] = values.where(df['c'] == np.nan, others=df['c'])





    share|improve this answer









    $endgroup$









    • 1




      $begingroup$
      Or np.where(pd.isnull(df.c), df.a * df.b, df.c)
      $endgroup$
      – Valentas
      Apr 16 '18 at 7:18





















    1












    $begingroup$

    Another option:



    df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B






    share|improve this answer









    $endgroup$





















      0












      $begingroup$

      hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"





      share








      New contributor




      user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      $endgroup$













        Your Answer





        StackExchange.ifUsing("editor", function () {
        return StackExchange.using("mathjaxEditing", function () {
        StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
        StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
        });
        });
        }, "mathjax-editing");

        StackExchange.ready(function() {
        var channelOptions = {
        tags: "".split(" "),
        id: "557"
        };
        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: false,
        noModals: true,
        showLowRepImageUploadWarning: true,
        reputationToPostImages: null,
        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%2fdatascience.stackexchange.com%2fquestions%2f17769%2fhow-to-fill-missing-value-based-on-other-columns-in-pandas-dataframe%23new-answer', 'question_page');
        }
        );

        Post as a guest















        Required, but never shown

























        4 Answers
        4






        active

        oldest

        votes








        4 Answers
        4






        active

        oldest

        votes









        active

        oldest

        votes






        active

        oldest

        votes









        10












        $begingroup$

        Assuming three columns of your dataframe is a, b and c. This is what you want:



        df['c'] = df.apply(
        lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
        axis=1
        )


        Full code:



        df = pd.DataFrame(
        np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
        columns=['a', 'b', 'c']
        )
        df['c'] = df.apply(
        lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
        axis=1
        )





        share|improve this answer









        $endgroup$


















          10












          $begingroup$

          Assuming three columns of your dataframe is a, b and c. This is what you want:



          df['c'] = df.apply(
          lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
          axis=1
          )


          Full code:



          df = pd.DataFrame(
          np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
          columns=['a', 'b', 'c']
          )
          df['c'] = df.apply(
          lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
          axis=1
          )





          share|improve this answer









          $endgroup$
















            10












            10








            10





            $begingroup$

            Assuming three columns of your dataframe is a, b and c. This is what you want:



            df['c'] = df.apply(
            lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
            axis=1
            )


            Full code:



            df = pd.DataFrame(
            np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
            columns=['a', 'b', 'c']
            )
            df['c'] = df.apply(
            lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
            axis=1
            )





            share|improve this answer









            $endgroup$



            Assuming three columns of your dataframe is a, b and c. This is what you want:



            df['c'] = df.apply(
            lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
            axis=1
            )


            Full code:



            df = pd.DataFrame(
            np.array([[1, 2, 3], [4, 5, np.nan], [7, 8, 9], [3, 2, np.nan], [5, 6, np.nan]]),
            columns=['a', 'b', 'c']
            )
            df['c'] = df.apply(
            lambda row: row['a']*row['b'] if np.isnan(row['c']) else row['c'],
            axis=1
            )






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 22 '17 at 13:15









            IcybladeIcyblade

            2,5111333




            2,5111333























                2












                $begingroup$

                Assuming that the three columns in your dataframe are a, b and c. Then you can do the required operation like this:



                values = df['a'] * df['b']
                df['c'] = values.where(df['c'] == np.nan, others=df['c'])





                share|improve this answer









                $endgroup$









                • 1




                  $begingroup$
                  Or np.where(pd.isnull(df.c), df.a * df.b, df.c)
                  $endgroup$
                  – Valentas
                  Apr 16 '18 at 7:18


















                2












                $begingroup$

                Assuming that the three columns in your dataframe are a, b and c. Then you can do the required operation like this:



                values = df['a'] * df['b']
                df['c'] = values.where(df['c'] == np.nan, others=df['c'])





                share|improve this answer









                $endgroup$









                • 1




                  $begingroup$
                  Or np.where(pd.isnull(df.c), df.a * df.b, df.c)
                  $endgroup$
                  – Valentas
                  Apr 16 '18 at 7:18
















                2












                2








                2





                $begingroup$

                Assuming that the three columns in your dataframe are a, b and c. Then you can do the required operation like this:



                values = df['a'] * df['b']
                df['c'] = values.where(df['c'] == np.nan, others=df['c'])





                share|improve this answer









                $endgroup$



                Assuming that the three columns in your dataframe are a, b and c. Then you can do the required operation like this:



                values = df['a'] * df['b']
                df['c'] = values.where(df['c'] == np.nan, others=df['c'])






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Mar 22 '17 at 13:45









                NainNain

                1,37951830




                1,37951830








                • 1




                  $begingroup$
                  Or np.where(pd.isnull(df.c), df.a * df.b, df.c)
                  $endgroup$
                  – Valentas
                  Apr 16 '18 at 7:18
















                • 1




                  $begingroup$
                  Or np.where(pd.isnull(df.c), df.a * df.b, df.c)
                  $endgroup$
                  – Valentas
                  Apr 16 '18 at 7:18










                1




                1




                $begingroup$
                Or np.where(pd.isnull(df.c), df.a * df.b, df.c)
                $endgroup$
                – Valentas
                Apr 16 '18 at 7:18






                $begingroup$
                Or np.where(pd.isnull(df.c), df.a * df.b, df.c)
                $endgroup$
                – Valentas
                Apr 16 '18 at 7:18













                1












                $begingroup$

                Another option:



                df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B






                share|improve this answer









                $endgroup$


















                  1












                  $begingroup$

                  Another option:



                  df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B






                  share|improve this answer









                  $endgroup$
















                    1












                    1








                    1





                    $begingroup$

                    Another option:



                    df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B






                    share|improve this answer









                    $endgroup$



                    Another option:



                    df.loc[(pd.isnull(df.C), 'C'] = df.A * df.B







                    share|improve this answer












                    share|improve this answer



                    share|improve this answer










                    answered Jun 4 '18 at 19:24









                    VishalVishal

                    1634




                    1634























                        0












                        $begingroup$

                        hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"





                        share








                        New contributor




                        user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                        Check out our Code of Conduct.






                        $endgroup$


















                          0












                          $begingroup$

                          hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"





                          share








                          New contributor




                          user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                          Check out our Code of Conduct.






                          $endgroup$
















                            0












                            0








                            0





                            $begingroup$

                            hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"





                            share








                            New contributor




                            user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            $endgroup$



                            hey even i have the same question . but what if the data i deal with is textual ? that is the condition is like "if 'ingredients' contains chicken then 'type'= non-veg"






                            share








                            New contributor




                            user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.








                            share


                            share






                            New contributor




                            user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.









                            answered 6 mins ago









                            user7389747user7389747

                            1




                            1




                            New contributor




                            user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.





                            New contributor





                            user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






                            user7389747 is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                            Check out our Code of Conduct.






























                                draft saved

                                draft discarded




















































                                Thanks for contributing an answer to Data Science Stack Exchange!


                                • 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.


                                Use MathJax to format equations. MathJax reference.


                                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%2fdatascience.stackexchange.com%2fquestions%2f17769%2fhow-to-fill-missing-value-based-on-other-columns-in-pandas-dataframe%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

                                How to label and detect the document text images

                                Vallis Paradisi

                                Tabula Rosettana