How to add a column which has three different values at varying lengths?











up vote
-3
down vote

favorite












Hi I have three lists "A","B","C" which contains 10 ,20 30 values respectively.I need to create a dataframe as shown below



Values                   Type
------ ------
ListA_values A
. A
. A
ListB_Values B
. B
.
ListC_Values C
. C
. C
. C


The columns here are Values and Type. Sorry Couldnt draw a better figure.

.










share|improve this question


























    up vote
    -3
    down vote

    favorite












    Hi I have three lists "A","B","C" which contains 10 ,20 30 values respectively.I need to create a dataframe as shown below



    Values                   Type
    ------ ------
    ListA_values A
    . A
    . A
    ListB_Values B
    . B
    .
    ListC_Values C
    . C
    . C
    . C


    The columns here are Values and Type. Sorry Couldnt draw a better figure.

    .










    share|improve this question
























      up vote
      -3
      down vote

      favorite









      up vote
      -3
      down vote

      favorite











      Hi I have three lists "A","B","C" which contains 10 ,20 30 values respectively.I need to create a dataframe as shown below



      Values                   Type
      ------ ------
      ListA_values A
      . A
      . A
      ListB_Values B
      . B
      .
      ListC_Values C
      . C
      . C
      . C


      The columns here are Values and Type. Sorry Couldnt draw a better figure.

      .










      share|improve this question













      Hi I have three lists "A","B","C" which contains 10 ,20 30 values respectively.I need to create a dataframe as shown below



      Values                   Type
      ------ ------
      ListA_values A
      . A
      . A
      ListB_Values B
      . B
      .
      ListC_Values C
      . C
      . C
      . C


      The columns here are Values and Type. Sorry Couldnt draw a better figure.

      .







      python pandas dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 20 at 12:31









      RAM SHANKER G

      146




      146
























          2 Answers
          2






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          df1 = pd.DataFrame({'Type': ['A']*100})
          df2 = pd.DataFrame({'Type': ['B']*75})
          df3 = pd.DataFrame({'Type': ['C']*80})

          #Concatenate the DataFrames
          df=df1.append(df2).append(df3).reset_index(drop=True)

          df.loc[df['Type']=='A','Values']='ListA_values'
          df.loc[df['Type']=='B','Values']='ListB_values'
          df.loc[df['Type']=='C','Values']='ListC_values'

          df=df[['Values','Type']]


          A more efficient way will be -



          df1 = pd.DataFrame({'Type': ['A']*100})
          df1.index = ['ListA_values'] * len(df1)
          df2 = pd.DataFrame({'Type': ['B']*75})
          df2.index = ['ListB_values'] * len(df2)
          df3 = pd.DataFrame({'Type': ['C']*80})
          df3.index = ['ListC_values'] * len(df3)

          #Concatenate the DataFrames
          df=df1.append(df2).append(df3)
          df['Values'] = df.index
          df=df.reset_index(drop=True)
          df=df[['Values','Type']]

          df
          Out[31]:
          Values Type
          0 ListA_values A
          1 ListA_values A
          2 ListA_values A
          3 ListA_values A
          4 ListA_values A
          5 ListA_values A
          6 ListA_values A
          .. ... ...
          252 ListC_values C
          253 ListC_values C
          254 ListC_values C





          share|improve this answer























          • What if i have 100 'A' values 75 'B' Values and 80 'C' values?
            – RAM SHANKER G
            Nov 20 at 13:06










          • I have made the changes. You can copy the code exactly.
            – cph_sto
            Nov 20 at 13:18










          • Would you mind accepting the answer if it helped you?
            – cph_sto
            Nov 22 at 20:21










          • Lol sure! :) Thanks a lot!
            – RAM SHANKER G
            Nov 23 at 13:30


















          up vote
          0
          down vote













          If you have lists, I'd create a dictionary and then use pd.concat + the default DataFrame constructor to make your DataFrame



          import pandas as pd

          # Whatever your values are, whatever the size.
          A = ['ListA_values']*40
          B = ['ListA_values']*80
          C = ['ListA_values']*90

          d = {'A': A, 'B': B, 'C': C}

          pd.concat([pd.DataFrame(v, columns=['Values']).assign(Type=k) for k,v in d.items()], ignore_index=True)


          Output:



                     Values Type
          0 ListA_values A
          1 ListA_values A
          2 ListA_values A
          3 ListA_values A
          4 ListA_values A
          5 ListA_values A
          .. ... ...
          206 ListA_values C
          207 ListA_values C
          208 ListA_values C
          209 ListA_values C





          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',
            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%2f53393049%2fhow-to-add-a-column-which-has-three-different-values-at-varying-lengths%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








            up vote
            0
            down vote



            accepted










            df1 = pd.DataFrame({'Type': ['A']*100})
            df2 = pd.DataFrame({'Type': ['B']*75})
            df3 = pd.DataFrame({'Type': ['C']*80})

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3).reset_index(drop=True)

            df.loc[df['Type']=='A','Values']='ListA_values'
            df.loc[df['Type']=='B','Values']='ListB_values'
            df.loc[df['Type']=='C','Values']='ListC_values'

            df=df[['Values','Type']]


            A more efficient way will be -



            df1 = pd.DataFrame({'Type': ['A']*100})
            df1.index = ['ListA_values'] * len(df1)
            df2 = pd.DataFrame({'Type': ['B']*75})
            df2.index = ['ListB_values'] * len(df2)
            df3 = pd.DataFrame({'Type': ['C']*80})
            df3.index = ['ListC_values'] * len(df3)

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3)
            df['Values'] = df.index
            df=df.reset_index(drop=True)
            df=df[['Values','Type']]

            df
            Out[31]:
            Values Type
            0 ListA_values A
            1 ListA_values A
            2 ListA_values A
            3 ListA_values A
            4 ListA_values A
            5 ListA_values A
            6 ListA_values A
            .. ... ...
            252 ListC_values C
            253 ListC_values C
            254 ListC_values C





            share|improve this answer























            • What if i have 100 'A' values 75 'B' Values and 80 'C' values?
              – RAM SHANKER G
              Nov 20 at 13:06










            • I have made the changes. You can copy the code exactly.
              – cph_sto
              Nov 20 at 13:18










            • Would you mind accepting the answer if it helped you?
              – cph_sto
              Nov 22 at 20:21










            • Lol sure! :) Thanks a lot!
              – RAM SHANKER G
              Nov 23 at 13:30















            up vote
            0
            down vote



            accepted










            df1 = pd.DataFrame({'Type': ['A']*100})
            df2 = pd.DataFrame({'Type': ['B']*75})
            df3 = pd.DataFrame({'Type': ['C']*80})

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3).reset_index(drop=True)

            df.loc[df['Type']=='A','Values']='ListA_values'
            df.loc[df['Type']=='B','Values']='ListB_values'
            df.loc[df['Type']=='C','Values']='ListC_values'

            df=df[['Values','Type']]


            A more efficient way will be -



            df1 = pd.DataFrame({'Type': ['A']*100})
            df1.index = ['ListA_values'] * len(df1)
            df2 = pd.DataFrame({'Type': ['B']*75})
            df2.index = ['ListB_values'] * len(df2)
            df3 = pd.DataFrame({'Type': ['C']*80})
            df3.index = ['ListC_values'] * len(df3)

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3)
            df['Values'] = df.index
            df=df.reset_index(drop=True)
            df=df[['Values','Type']]

            df
            Out[31]:
            Values Type
            0 ListA_values A
            1 ListA_values A
            2 ListA_values A
            3 ListA_values A
            4 ListA_values A
            5 ListA_values A
            6 ListA_values A
            .. ... ...
            252 ListC_values C
            253 ListC_values C
            254 ListC_values C





            share|improve this answer























            • What if i have 100 'A' values 75 'B' Values and 80 'C' values?
              – RAM SHANKER G
              Nov 20 at 13:06










            • I have made the changes. You can copy the code exactly.
              – cph_sto
              Nov 20 at 13:18










            • Would you mind accepting the answer if it helped you?
              – cph_sto
              Nov 22 at 20:21










            • Lol sure! :) Thanks a lot!
              – RAM SHANKER G
              Nov 23 at 13:30













            up vote
            0
            down vote



            accepted







            up vote
            0
            down vote



            accepted






            df1 = pd.DataFrame({'Type': ['A']*100})
            df2 = pd.DataFrame({'Type': ['B']*75})
            df3 = pd.DataFrame({'Type': ['C']*80})

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3).reset_index(drop=True)

            df.loc[df['Type']=='A','Values']='ListA_values'
            df.loc[df['Type']=='B','Values']='ListB_values'
            df.loc[df['Type']=='C','Values']='ListC_values'

            df=df[['Values','Type']]


            A more efficient way will be -



            df1 = pd.DataFrame({'Type': ['A']*100})
            df1.index = ['ListA_values'] * len(df1)
            df2 = pd.DataFrame({'Type': ['B']*75})
            df2.index = ['ListB_values'] * len(df2)
            df3 = pd.DataFrame({'Type': ['C']*80})
            df3.index = ['ListC_values'] * len(df3)

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3)
            df['Values'] = df.index
            df=df.reset_index(drop=True)
            df=df[['Values','Type']]

            df
            Out[31]:
            Values Type
            0 ListA_values A
            1 ListA_values A
            2 ListA_values A
            3 ListA_values A
            4 ListA_values A
            5 ListA_values A
            6 ListA_values A
            .. ... ...
            252 ListC_values C
            253 ListC_values C
            254 ListC_values C





            share|improve this answer














            df1 = pd.DataFrame({'Type': ['A']*100})
            df2 = pd.DataFrame({'Type': ['B']*75})
            df3 = pd.DataFrame({'Type': ['C']*80})

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3).reset_index(drop=True)

            df.loc[df['Type']=='A','Values']='ListA_values'
            df.loc[df['Type']=='B','Values']='ListB_values'
            df.loc[df['Type']=='C','Values']='ListC_values'

            df=df[['Values','Type']]


            A more efficient way will be -



            df1 = pd.DataFrame({'Type': ['A']*100})
            df1.index = ['ListA_values'] * len(df1)
            df2 = pd.DataFrame({'Type': ['B']*75})
            df2.index = ['ListB_values'] * len(df2)
            df3 = pd.DataFrame({'Type': ['C']*80})
            df3.index = ['ListC_values'] * len(df3)

            #Concatenate the DataFrames
            df=df1.append(df2).append(df3)
            df['Values'] = df.index
            df=df.reset_index(drop=True)
            df=df[['Values','Type']]

            df
            Out[31]:
            Values Type
            0 ListA_values A
            1 ListA_values A
            2 ListA_values A
            3 ListA_values A
            4 ListA_values A
            5 ListA_values A
            6 ListA_values A
            .. ... ...
            252 ListC_values C
            253 ListC_values C
            254 ListC_values C






            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Nov 20 at 15:06

























            answered Nov 20 at 12:51









            cph_sto

            667216




            667216












            • What if i have 100 'A' values 75 'B' Values and 80 'C' values?
              – RAM SHANKER G
              Nov 20 at 13:06










            • I have made the changes. You can copy the code exactly.
              – cph_sto
              Nov 20 at 13:18










            • Would you mind accepting the answer if it helped you?
              – cph_sto
              Nov 22 at 20:21










            • Lol sure! :) Thanks a lot!
              – RAM SHANKER G
              Nov 23 at 13:30


















            • What if i have 100 'A' values 75 'B' Values and 80 'C' values?
              – RAM SHANKER G
              Nov 20 at 13:06










            • I have made the changes. You can copy the code exactly.
              – cph_sto
              Nov 20 at 13:18










            • Would you mind accepting the answer if it helped you?
              – cph_sto
              Nov 22 at 20:21










            • Lol sure! :) Thanks a lot!
              – RAM SHANKER G
              Nov 23 at 13:30
















            What if i have 100 'A' values 75 'B' Values and 80 'C' values?
            – RAM SHANKER G
            Nov 20 at 13:06




            What if i have 100 'A' values 75 'B' Values and 80 'C' values?
            – RAM SHANKER G
            Nov 20 at 13:06












            I have made the changes. You can copy the code exactly.
            – cph_sto
            Nov 20 at 13:18




            I have made the changes. You can copy the code exactly.
            – cph_sto
            Nov 20 at 13:18












            Would you mind accepting the answer if it helped you?
            – cph_sto
            Nov 22 at 20:21




            Would you mind accepting the answer if it helped you?
            – cph_sto
            Nov 22 at 20:21












            Lol sure! :) Thanks a lot!
            – RAM SHANKER G
            Nov 23 at 13:30




            Lol sure! :) Thanks a lot!
            – RAM SHANKER G
            Nov 23 at 13:30












            up vote
            0
            down vote













            If you have lists, I'd create a dictionary and then use pd.concat + the default DataFrame constructor to make your DataFrame



            import pandas as pd

            # Whatever your values are, whatever the size.
            A = ['ListA_values']*40
            B = ['ListA_values']*80
            C = ['ListA_values']*90

            d = {'A': A, 'B': B, 'C': C}

            pd.concat([pd.DataFrame(v, columns=['Values']).assign(Type=k) for k,v in d.items()], ignore_index=True)


            Output:



                       Values Type
            0 ListA_values A
            1 ListA_values A
            2 ListA_values A
            3 ListA_values A
            4 ListA_values A
            5 ListA_values A
            .. ... ...
            206 ListA_values C
            207 ListA_values C
            208 ListA_values C
            209 ListA_values C





            share|improve this answer

























              up vote
              0
              down vote













              If you have lists, I'd create a dictionary and then use pd.concat + the default DataFrame constructor to make your DataFrame



              import pandas as pd

              # Whatever your values are, whatever the size.
              A = ['ListA_values']*40
              B = ['ListA_values']*80
              C = ['ListA_values']*90

              d = {'A': A, 'B': B, 'C': C}

              pd.concat([pd.DataFrame(v, columns=['Values']).assign(Type=k) for k,v in d.items()], ignore_index=True)


              Output:



                         Values Type
              0 ListA_values A
              1 ListA_values A
              2 ListA_values A
              3 ListA_values A
              4 ListA_values A
              5 ListA_values A
              .. ... ...
              206 ListA_values C
              207 ListA_values C
              208 ListA_values C
              209 ListA_values C





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                If you have lists, I'd create a dictionary and then use pd.concat + the default DataFrame constructor to make your DataFrame



                import pandas as pd

                # Whatever your values are, whatever the size.
                A = ['ListA_values']*40
                B = ['ListA_values']*80
                C = ['ListA_values']*90

                d = {'A': A, 'B': B, 'C': C}

                pd.concat([pd.DataFrame(v, columns=['Values']).assign(Type=k) for k,v in d.items()], ignore_index=True)


                Output:



                           Values Type
                0 ListA_values A
                1 ListA_values A
                2 ListA_values A
                3 ListA_values A
                4 ListA_values A
                5 ListA_values A
                .. ... ...
                206 ListA_values C
                207 ListA_values C
                208 ListA_values C
                209 ListA_values C





                share|improve this answer












                If you have lists, I'd create a dictionary and then use pd.concat + the default DataFrame constructor to make your DataFrame



                import pandas as pd

                # Whatever your values are, whatever the size.
                A = ['ListA_values']*40
                B = ['ListA_values']*80
                C = ['ListA_values']*90

                d = {'A': A, 'B': B, 'C': C}

                pd.concat([pd.DataFrame(v, columns=['Values']).assign(Type=k) for k,v in d.items()], ignore_index=True)


                Output:



                           Values Type
                0 ListA_values A
                1 ListA_values A
                2 ListA_values A
                3 ListA_values A
                4 ListA_values A
                5 ListA_values A
                .. ... ...
                206 ListA_values C
                207 ListA_values C
                208 ListA_values C
                209 ListA_values C






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 20 at 15:20









                ALollz

                10.9k31334




                10.9k31334






























                    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%2f53393049%2fhow-to-add-a-column-which-has-three-different-values-at-varying-lengths%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'