How to creat a dataframe of columns each with 'n' empty values












0















I am trying to create a dataframe of empty values. My code is



df['Voc_inIV'] = np.nan
df['Isc_inIV'] = np.nan
df['Voc_error'] = np.nan
df['Isc_error'] = np.nan
df['Pmpp_inIV'] = np.nan
df['ff'] = np.nan
df['v_at_Isc'] = np.nan
df['i_at_voc'] = np.nan


My above approach works. But this does not look good.
Is there a better approach than this? I mean, I did not like repeating np.nan all the times. Moreover, my dataframe should have n rows.










share|improve this question





























    0















    I am trying to create a dataframe of empty values. My code is



    df['Voc_inIV'] = np.nan
    df['Isc_inIV'] = np.nan
    df['Voc_error'] = np.nan
    df['Isc_error'] = np.nan
    df['Pmpp_inIV'] = np.nan
    df['ff'] = np.nan
    df['v_at_Isc'] = np.nan
    df['i_at_voc'] = np.nan


    My above approach works. But this does not look good.
    Is there a better approach than this? I mean, I did not like repeating np.nan all the times. Moreover, my dataframe should have n rows.










    share|improve this question



























      0












      0








      0








      I am trying to create a dataframe of empty values. My code is



      df['Voc_inIV'] = np.nan
      df['Isc_inIV'] = np.nan
      df['Voc_error'] = np.nan
      df['Isc_error'] = np.nan
      df['Pmpp_inIV'] = np.nan
      df['ff'] = np.nan
      df['v_at_Isc'] = np.nan
      df['i_at_voc'] = np.nan


      My above approach works. But this does not look good.
      Is there a better approach than this? I mean, I did not like repeating np.nan all the times. Moreover, my dataframe should have n rows.










      share|improve this question
















      I am trying to create a dataframe of empty values. My code is



      df['Voc_inIV'] = np.nan
      df['Isc_inIV'] = np.nan
      df['Voc_error'] = np.nan
      df['Isc_error'] = np.nan
      df['Pmpp_inIV'] = np.nan
      df['ff'] = np.nan
      df['v_at_Isc'] = np.nan
      df['i_at_voc'] = np.nan


      My above approach works. But this does not look good.
      Is there a better approach than this? I mean, I did not like repeating np.nan all the times. Moreover, my dataframe should have n rows.







      python-3.x pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 25 '18 at 5:24







      Msquare

















      asked Nov 25 '18 at 4:37









      MsquareMsquare

      295




      295
























          2 Answers
          2






          active

          oldest

          votes


















          1














          To create an empty dataframe, just don't pass any data to the data argument, but pass your column names to columns:



          cols = ['Voc_inIV','Isc_inIV','Voc_error','Isc_error','Pmpp_inIV','ff','v_at_Isc','i_at_voc']

          df = pd.DataFrame(columns=cols)


          Giving you:



          >>> df
          Empty DataFrame
          Columns: [Voc_inIV, Isc_inIV, Voc_error, Isc_error, Pmpp_inIV, ff, v_at_Isc, i_at_voc]
          Index:


          Edit: If you need it to have n rows, pass an index of range(n):



          n = 5

          df = pd.DataFrame(columns=cols, index=range(n))

          >>> df
          Voc_inIV Isc_inIV Voc_error Isc_error Pmpp_inIV ff v_at_Isc i_at_voc
          0 NaN NaN NaN NaN NaN NaN NaN NaN
          1 NaN NaN NaN NaN NaN NaN NaN NaN
          2 NaN NaN NaN NaN NaN NaN NaN NaN
          3 NaN NaN NaN NaN NaN NaN NaN NaN
          4 NaN NaN NaN NaN NaN NaN NaN NaN





          share|improve this answer


























          • But, how to assign a predefined size to df?

            – Msquare
            Nov 25 '18 at 5:23













          • See my edits, that should work :)

            – sacuL
            Nov 25 '18 at 15:27



















          0














          Here is another approach, helping you specifically with not repeating np.nan:



          df = pd.DataFrame()
          cols = ['Voc_inIV', 'Isc_inIV', 'Voc_error', 'Isc_error', 'Pmpp_inIV', 'ff', 'v_at_Isc', 'i_at_voc']
          for col in range(len(cols)):
          df[cols[col]] = np.nan





          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%2f53464689%2fhow-to-creat-a-dataframe-of-columns-each-with-n-empty-values%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














            To create an empty dataframe, just don't pass any data to the data argument, but pass your column names to columns:



            cols = ['Voc_inIV','Isc_inIV','Voc_error','Isc_error','Pmpp_inIV','ff','v_at_Isc','i_at_voc']

            df = pd.DataFrame(columns=cols)


            Giving you:



            >>> df
            Empty DataFrame
            Columns: [Voc_inIV, Isc_inIV, Voc_error, Isc_error, Pmpp_inIV, ff, v_at_Isc, i_at_voc]
            Index:


            Edit: If you need it to have n rows, pass an index of range(n):



            n = 5

            df = pd.DataFrame(columns=cols, index=range(n))

            >>> df
            Voc_inIV Isc_inIV Voc_error Isc_error Pmpp_inIV ff v_at_Isc i_at_voc
            0 NaN NaN NaN NaN NaN NaN NaN NaN
            1 NaN NaN NaN NaN NaN NaN NaN NaN
            2 NaN NaN NaN NaN NaN NaN NaN NaN
            3 NaN NaN NaN NaN NaN NaN NaN NaN
            4 NaN NaN NaN NaN NaN NaN NaN NaN





            share|improve this answer


























            • But, how to assign a predefined size to df?

              – Msquare
              Nov 25 '18 at 5:23













            • See my edits, that should work :)

              – sacuL
              Nov 25 '18 at 15:27
















            1














            To create an empty dataframe, just don't pass any data to the data argument, but pass your column names to columns:



            cols = ['Voc_inIV','Isc_inIV','Voc_error','Isc_error','Pmpp_inIV','ff','v_at_Isc','i_at_voc']

            df = pd.DataFrame(columns=cols)


            Giving you:



            >>> df
            Empty DataFrame
            Columns: [Voc_inIV, Isc_inIV, Voc_error, Isc_error, Pmpp_inIV, ff, v_at_Isc, i_at_voc]
            Index:


            Edit: If you need it to have n rows, pass an index of range(n):



            n = 5

            df = pd.DataFrame(columns=cols, index=range(n))

            >>> df
            Voc_inIV Isc_inIV Voc_error Isc_error Pmpp_inIV ff v_at_Isc i_at_voc
            0 NaN NaN NaN NaN NaN NaN NaN NaN
            1 NaN NaN NaN NaN NaN NaN NaN NaN
            2 NaN NaN NaN NaN NaN NaN NaN NaN
            3 NaN NaN NaN NaN NaN NaN NaN NaN
            4 NaN NaN NaN NaN NaN NaN NaN NaN





            share|improve this answer


























            • But, how to assign a predefined size to df?

              – Msquare
              Nov 25 '18 at 5:23













            • See my edits, that should work :)

              – sacuL
              Nov 25 '18 at 15:27














            1












            1








            1







            To create an empty dataframe, just don't pass any data to the data argument, but pass your column names to columns:



            cols = ['Voc_inIV','Isc_inIV','Voc_error','Isc_error','Pmpp_inIV','ff','v_at_Isc','i_at_voc']

            df = pd.DataFrame(columns=cols)


            Giving you:



            >>> df
            Empty DataFrame
            Columns: [Voc_inIV, Isc_inIV, Voc_error, Isc_error, Pmpp_inIV, ff, v_at_Isc, i_at_voc]
            Index:


            Edit: If you need it to have n rows, pass an index of range(n):



            n = 5

            df = pd.DataFrame(columns=cols, index=range(n))

            >>> df
            Voc_inIV Isc_inIV Voc_error Isc_error Pmpp_inIV ff v_at_Isc i_at_voc
            0 NaN NaN NaN NaN NaN NaN NaN NaN
            1 NaN NaN NaN NaN NaN NaN NaN NaN
            2 NaN NaN NaN NaN NaN NaN NaN NaN
            3 NaN NaN NaN NaN NaN NaN NaN NaN
            4 NaN NaN NaN NaN NaN NaN NaN NaN





            share|improve this answer















            To create an empty dataframe, just don't pass any data to the data argument, but pass your column names to columns:



            cols = ['Voc_inIV','Isc_inIV','Voc_error','Isc_error','Pmpp_inIV','ff','v_at_Isc','i_at_voc']

            df = pd.DataFrame(columns=cols)


            Giving you:



            >>> df
            Empty DataFrame
            Columns: [Voc_inIV, Isc_inIV, Voc_error, Isc_error, Pmpp_inIV, ff, v_at_Isc, i_at_voc]
            Index:


            Edit: If you need it to have n rows, pass an index of range(n):



            n = 5

            df = pd.DataFrame(columns=cols, index=range(n))

            >>> df
            Voc_inIV Isc_inIV Voc_error Isc_error Pmpp_inIV ff v_at_Isc i_at_voc
            0 NaN NaN NaN NaN NaN NaN NaN NaN
            1 NaN NaN NaN NaN NaN NaN NaN NaN
            2 NaN NaN NaN NaN NaN NaN NaN NaN
            3 NaN NaN NaN NaN NaN NaN NaN NaN
            4 NaN NaN NaN NaN NaN NaN NaN NaN






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 25 '18 at 15:27

























            answered Nov 25 '18 at 4:42









            sacuLsacuL

            30.5k41942




            30.5k41942













            • But, how to assign a predefined size to df?

              – Msquare
              Nov 25 '18 at 5:23













            • See my edits, that should work :)

              – sacuL
              Nov 25 '18 at 15:27



















            • But, how to assign a predefined size to df?

              – Msquare
              Nov 25 '18 at 5:23













            • See my edits, that should work :)

              – sacuL
              Nov 25 '18 at 15:27

















            But, how to assign a predefined size to df?

            – Msquare
            Nov 25 '18 at 5:23







            But, how to assign a predefined size to df?

            – Msquare
            Nov 25 '18 at 5:23















            See my edits, that should work :)

            – sacuL
            Nov 25 '18 at 15:27





            See my edits, that should work :)

            – sacuL
            Nov 25 '18 at 15:27













            0














            Here is another approach, helping you specifically with not repeating np.nan:



            df = pd.DataFrame()
            cols = ['Voc_inIV', 'Isc_inIV', 'Voc_error', 'Isc_error', 'Pmpp_inIV', 'ff', 'v_at_Isc', 'i_at_voc']
            for col in range(len(cols)):
            df[cols[col]] = np.nan





            share|improve this answer




























              0














              Here is another approach, helping you specifically with not repeating np.nan:



              df = pd.DataFrame()
              cols = ['Voc_inIV', 'Isc_inIV', 'Voc_error', 'Isc_error', 'Pmpp_inIV', 'ff', 'v_at_Isc', 'i_at_voc']
              for col in range(len(cols)):
              df[cols[col]] = np.nan





              share|improve this answer


























                0












                0








                0







                Here is another approach, helping you specifically with not repeating np.nan:



                df = pd.DataFrame()
                cols = ['Voc_inIV', 'Isc_inIV', 'Voc_error', 'Isc_error', 'Pmpp_inIV', 'ff', 'v_at_Isc', 'i_at_voc']
                for col in range(len(cols)):
                df[cols[col]] = np.nan





                share|improve this answer













                Here is another approach, helping you specifically with not repeating np.nan:



                df = pd.DataFrame()
                cols = ['Voc_inIV', 'Isc_inIV', 'Voc_error', 'Isc_error', 'Pmpp_inIV', 'ff', 'v_at_Isc', 'i_at_voc']
                for col in range(len(cols)):
                df[cols[col]] = np.nan






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 25 '18 at 4:46









                Koray TugayKoray Tugay

                8,99126116223




                8,99126116223






























                    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%2f53464689%2fhow-to-creat-a-dataframe-of-columns-each-with-n-empty-values%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

                    404 Error Contact Form 7 ajax form submitting

                    How to know if a Active Directory user can login interactively

                    TypeError: fit_transform() missing 1 required positional argument: 'X'