How to slice out column names based on column into row of new dataframe?












0















I have a df that looks like this



data.answers.1542213647002.subItemType   data.answers.1542213647002.value.1542213647003               
thank you for the response TRUE


How do I slice out the column name only for columns that have the string .value. and the column has the value TRUE into a new df like so?:



new_df



old_column_names
data.answers.1542213647002.value.1542213647003


I have roughly 100 more columns with .value. in it but not all of them have TRUE in them as values.










share|improve this question





























    0















    I have a df that looks like this



    data.answers.1542213647002.subItemType   data.answers.1542213647002.value.1542213647003               
    thank you for the response TRUE


    How do I slice out the column name only for columns that have the string .value. and the column has the value TRUE into a new df like so?:



    new_df



    old_column_names
    data.answers.1542213647002.value.1542213647003


    I have roughly 100 more columns with .value. in it but not all of them have TRUE in them as values.










    share|improve this question



























      0












      0








      0








      I have a df that looks like this



      data.answers.1542213647002.subItemType   data.answers.1542213647002.value.1542213647003               
      thank you for the response TRUE


      How do I slice out the column name only for columns that have the string .value. and the column has the value TRUE into a new df like so?:



      new_df



      old_column_names
      data.answers.1542213647002.value.1542213647003


      I have roughly 100 more columns with .value. in it but not all of them have TRUE in them as values.










      share|improve this question
















      I have a df that looks like this



      data.answers.1542213647002.subItemType   data.answers.1542213647002.value.1542213647003               
      thank you for the response TRUE


      How do I slice out the column name only for columns that have the string .value. and the column has the value TRUE into a new df like so?:



      new_df



      old_column_names
      data.answers.1542213647002.value.1542213647003


      I have roughly 100 more columns with .value. in it but not all of them have TRUE in them as values.







      python-3.x pandas






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Jan 19 at 18:35







      RustyShackleford

















      asked Jan 19 at 18:15









      RustyShacklefordRustyShackleford

      1,178621




      1,178621
























          1 Answer
          1






          active

          oldest

          votes


















          1














          assume this sample df:



          df = pd.DataFrame({'col':[1,2]*5,
          'col2.value.something':[True,False]*5,
          'col3.value.something':[5]*10,
          'col4':[True]*10})


          then



          # boolean indexing with stack
          new = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

          # drop duplicates
          new = new.drop(columns=0).drop_duplicates()

          1
          0 col2.value.something





          share|improve this answer
























          • Thank you for the response: I get error ValueError: Wrong number of items passed 2, placement implies 1 when I run your line. I did it this way: new_df['new_col'] = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

            – RustyShackleford
            Jan 19 at 18:49











          • @RustyShackleford You are getting that error because you are trying to pass a dataframe with two columns and set it to one column in another dataframe

            – Chris
            Jan 19 at 18:51













          • anyway I can do your code the way I set it up?

            – RustyShackleford
            Jan 19 at 18:54











          • @RustyShackleford sure but I need some more information: is new_df already a dataframe or do you want that to be the variable name?

            – Chris
            Jan 19 at 18:56






          • 1





            it is not a new df. However with your solution I recreated the dataframe with your logic and added the new rows. I dont think we need to rework the solution. Thank you so much

            – RustyShackleford
            Jan 19 at 19:01











          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%2f54270006%2fhow-to-slice-out-column-names-based-on-column-into-row-of-new-dataframe%23new-answer', 'question_page');
          }
          );

          Post as a guest















          Required, but never shown

























          1 Answer
          1






          active

          oldest

          votes








          1 Answer
          1






          active

          oldest

          votes









          active

          oldest

          votes






          active

          oldest

          votes









          1














          assume this sample df:



          df = pd.DataFrame({'col':[1,2]*5,
          'col2.value.something':[True,False]*5,
          'col3.value.something':[5]*10,
          'col4':[True]*10})


          then



          # boolean indexing with stack
          new = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

          # drop duplicates
          new = new.drop(columns=0).drop_duplicates()

          1
          0 col2.value.something





          share|improve this answer
























          • Thank you for the response: I get error ValueError: Wrong number of items passed 2, placement implies 1 when I run your line. I did it this way: new_df['new_col'] = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

            – RustyShackleford
            Jan 19 at 18:49











          • @RustyShackleford You are getting that error because you are trying to pass a dataframe with two columns and set it to one column in another dataframe

            – Chris
            Jan 19 at 18:51













          • anyway I can do your code the way I set it up?

            – RustyShackleford
            Jan 19 at 18:54











          • @RustyShackleford sure but I need some more information: is new_df already a dataframe or do you want that to be the variable name?

            – Chris
            Jan 19 at 18:56






          • 1





            it is not a new df. However with your solution I recreated the dataframe with your logic and added the new rows. I dont think we need to rework the solution. Thank you so much

            – RustyShackleford
            Jan 19 at 19:01
















          1














          assume this sample df:



          df = pd.DataFrame({'col':[1,2]*5,
          'col2.value.something':[True,False]*5,
          'col3.value.something':[5]*10,
          'col4':[True]*10})


          then



          # boolean indexing with stack
          new = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

          # drop duplicates
          new = new.drop(columns=0).drop_duplicates()

          1
          0 col2.value.something





          share|improve this answer
























          • Thank you for the response: I get error ValueError: Wrong number of items passed 2, placement implies 1 when I run your line. I did it this way: new_df['new_col'] = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

            – RustyShackleford
            Jan 19 at 18:49











          • @RustyShackleford You are getting that error because you are trying to pass a dataframe with two columns and set it to one column in another dataframe

            – Chris
            Jan 19 at 18:51













          • anyway I can do your code the way I set it up?

            – RustyShackleford
            Jan 19 at 18:54











          • @RustyShackleford sure but I need some more information: is new_df already a dataframe or do you want that to be the variable name?

            – Chris
            Jan 19 at 18:56






          • 1





            it is not a new df. However with your solution I recreated the dataframe with your logic and added the new rows. I dont think we need to rework the solution. Thank you so much

            – RustyShackleford
            Jan 19 at 19:01














          1












          1








          1







          assume this sample df:



          df = pd.DataFrame({'col':[1,2]*5,
          'col2.value.something':[True,False]*5,
          'col3.value.something':[5]*10,
          'col4':[True]*10})


          then



          # boolean indexing with stack
          new = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

          # drop duplicates
          new = new.drop(columns=0).drop_duplicates()

          1
          0 col2.value.something





          share|improve this answer













          assume this sample df:



          df = pd.DataFrame({'col':[1,2]*5,
          'col2.value.something':[True,False]*5,
          'col3.value.something':[5]*10,
          'col4':[True]*10})


          then



          # boolean indexing with stack
          new = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

          # drop duplicates
          new = new.drop(columns=0).drop_duplicates()

          1
          0 col2.value.something






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Jan 19 at 18:44









          ChrisChris

          2,4882420




          2,4882420













          • Thank you for the response: I get error ValueError: Wrong number of items passed 2, placement implies 1 when I run your line. I did it this way: new_df['new_col'] = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

            – RustyShackleford
            Jan 19 at 18:49











          • @RustyShackleford You are getting that error because you are trying to pass a dataframe with two columns and set it to one column in another dataframe

            – Chris
            Jan 19 at 18:51













          • anyway I can do your code the way I set it up?

            – RustyShackleford
            Jan 19 at 18:54











          • @RustyShackleford sure but I need some more information: is new_df already a dataframe or do you want that to be the variable name?

            – Chris
            Jan 19 at 18:56






          • 1





            it is not a new df. However with your solution I recreated the dataframe with your logic and added the new rows. I dont think we need to rework the solution. Thank you so much

            – RustyShackleford
            Jan 19 at 19:01



















          • Thank you for the response: I get error ValueError: Wrong number of items passed 2, placement implies 1 when I run your line. I did it this way: new_df['new_col'] = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

            – RustyShackleford
            Jan 19 at 18:49











          • @RustyShackleford You are getting that error because you are trying to pass a dataframe with two columns and set it to one column in another dataframe

            – Chris
            Jan 19 at 18:51













          • anyway I can do your code the way I set it up?

            – RustyShackleford
            Jan 19 at 18:54











          • @RustyShackleford sure but I need some more information: is new_df already a dataframe or do you want that to be the variable name?

            – Chris
            Jan 19 at 18:56






          • 1





            it is not a new df. However with your solution I recreated the dataframe with your logic and added the new rows. I dont think we need to rework the solution. Thank you so much

            – RustyShackleford
            Jan 19 at 19:01

















          Thank you for the response: I get error ValueError: Wrong number of items passed 2, placement implies 1 when I run your line. I did it this way: new_df['new_col'] = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

          – RustyShackleford
          Jan 19 at 18:49





          Thank you for the response: I get error ValueError: Wrong number of items passed 2, placement implies 1 when I run your line. I did it this way: new_df['new_col'] = pd.DataFrame(list(df[((df==True) & (df.columns.str.contains('.value.')))].stack().index))

          – RustyShackleford
          Jan 19 at 18:49













          @RustyShackleford You are getting that error because you are trying to pass a dataframe with two columns and set it to one column in another dataframe

          – Chris
          Jan 19 at 18:51







          @RustyShackleford You are getting that error because you are trying to pass a dataframe with two columns and set it to one column in another dataframe

          – Chris
          Jan 19 at 18:51















          anyway I can do your code the way I set it up?

          – RustyShackleford
          Jan 19 at 18:54





          anyway I can do your code the way I set it up?

          – RustyShackleford
          Jan 19 at 18:54













          @RustyShackleford sure but I need some more information: is new_df already a dataframe or do you want that to be the variable name?

          – Chris
          Jan 19 at 18:56





          @RustyShackleford sure but I need some more information: is new_df already a dataframe or do you want that to be the variable name?

          – Chris
          Jan 19 at 18:56




          1




          1





          it is not a new df. However with your solution I recreated the dataframe with your logic and added the new rows. I dont think we need to rework the solution. Thank you so much

          – RustyShackleford
          Jan 19 at 19:01





          it is not a new df. However with your solution I recreated the dataframe with your logic and added the new rows. I dont think we need to rework the solution. Thank you so much

          – RustyShackleford
          Jan 19 at 19:01


















          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%2f54270006%2fhow-to-slice-out-column-names-based-on-column-into-row-of-new-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

          Liquibase includeAll doesn't find base path

          How to use setInterval in EJS file?

          Petrus Granier-Deferre