Create a python dataframe using string of variable











up vote
0
down vote

favorite












I have a function for running a logistic regression model. I would like to permanently save the dataframe generated within (cf) and amend it's name.



def model(ind, dep):
global cf
ind.fillna(0)
#some modelling code

#create confuson matix

cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
cf.index = models
cf.columns = models
print(cf)
cf.plot.barh()


"cf_" + str(ind) = cf

return "cf_" + str(ind)

model(X_tv, y_combo)
model(X_tv_chan, y_combo)


I get this error



File "", line 64
"cf_" + str(ind) = coeff
^
SyntaxError: can't assign to operator



is there something wrong with how I am trying to create the dataframe?



"cf_" + str(ind) = cf









share|improve this question


























    up vote
    0
    down vote

    favorite












    I have a function for running a logistic regression model. I would like to permanently save the dataframe generated within (cf) and amend it's name.



    def model(ind, dep):
    global cf
    ind.fillna(0)
    #some modelling code

    #create confuson matix

    cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
    cf.index = models
    cf.columns = models
    print(cf)
    cf.plot.barh()


    "cf_" + str(ind) = cf

    return "cf_" + str(ind)

    model(X_tv, y_combo)
    model(X_tv_chan, y_combo)


    I get this error



    File "", line 64
    "cf_" + str(ind) = coeff
    ^
    SyntaxError: can't assign to operator



    is there something wrong with how I am trying to create the dataframe?



    "cf_" + str(ind) = cf









    share|improve this question
























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I have a function for running a logistic regression model. I would like to permanently save the dataframe generated within (cf) and amend it's name.



      def model(ind, dep):
      global cf
      ind.fillna(0)
      #some modelling code

      #create confuson matix

      cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
      cf.index = models
      cf.columns = models
      print(cf)
      cf.plot.barh()


      "cf_" + str(ind) = cf

      return "cf_" + str(ind)

      model(X_tv, y_combo)
      model(X_tv_chan, y_combo)


      I get this error



      File "", line 64
      "cf_" + str(ind) = coeff
      ^
      SyntaxError: can't assign to operator



      is there something wrong with how I am trying to create the dataframe?



      "cf_" + str(ind) = cf









      share|improve this question













      I have a function for running a logistic regression model. I would like to permanently save the dataframe generated within (cf) and amend it's name.



      def model(ind, dep):
      global cf
      ind.fillna(0)
      #some modelling code

      #create confuson matix

      cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
      cf.index = models
      cf.columns = models
      print(cf)
      cf.plot.barh()


      "cf_" + str(ind) = cf

      return "cf_" + str(ind)

      model(X_tv, y_combo)
      model(X_tv_chan, y_combo)


      I get this error



      File "", line 64
      "cf_" + str(ind) = coeff
      ^
      SyntaxError: can't assign to operator



      is there something wrong with how I am trying to create the dataframe?



      "cf_" + str(ind) = cf






      python string dataframe






      share|improve this question













      share|improve this question











      share|improve this question




      share|improve this question










      asked Nov 9 at 11:05









      James Adams

      891213




      891213
























          1 Answer
          1






          active

          oldest

          votes

















          up vote
          1
          down vote













          Yes. This line doesn't create a new variable:



          "cf_" + str(ind) = cf


          Strings are immutable. You can't "assign a dataframe to a string" either, I'm not even sure what this is attempting to achieve. It's also good practice to avoid global variables.



          Just return your dataframe and assign to an explicit variable name. If you are set on changing your variable name, use a dictionary and dict.pop:



          def model(ind, dep):
          # ...
          cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
          cf.index = models
          cf.columns = models
          # ...
          return cf

          dfs = {}

          dfs['ind'] = model(X_tv, y_combo) # identify dataframe with key 'ind'
          dfs['cf_ind'] = dfs.pop('ind') # rename identifier to 'cf_ind'





          share|improve this answer





















          • Thanks. I have a number of dataframes I want to pull out of the function and save. In the end I have used the global options and then returned then. Then in between each call of the function I have appended the new table.model (X_tv, y_combo, 'just_trans') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff) model(X_tv_cattot, y_combo, 'trans_cattotals') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff)
            – James Adams
            Nov 9 at 15:54












          • I want to pull out of the function and save. That's what this does: dfs['ind'] = model(X_tv, y_combo). Please don't post code in comments, edit your question to clarify your question.
            – jpp
            Nov 9 at 15:55













          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%2f53224522%2fcreate-a-python-dataframe-using-string-of-variable%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








          up vote
          1
          down vote













          Yes. This line doesn't create a new variable:



          "cf_" + str(ind) = cf


          Strings are immutable. You can't "assign a dataframe to a string" either, I'm not even sure what this is attempting to achieve. It's also good practice to avoid global variables.



          Just return your dataframe and assign to an explicit variable name. If you are set on changing your variable name, use a dictionary and dict.pop:



          def model(ind, dep):
          # ...
          cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
          cf.index = models
          cf.columns = models
          # ...
          return cf

          dfs = {}

          dfs['ind'] = model(X_tv, y_combo) # identify dataframe with key 'ind'
          dfs['cf_ind'] = dfs.pop('ind') # rename identifier to 'cf_ind'





          share|improve this answer





















          • Thanks. I have a number of dataframes I want to pull out of the function and save. In the end I have used the global options and then returned then. Then in between each call of the function I have appended the new table.model (X_tv, y_combo, 'just_trans') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff) model(X_tv_cattot, y_combo, 'trans_cattotals') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff)
            – James Adams
            Nov 9 at 15:54












          • I want to pull out of the function and save. That's what this does: dfs['ind'] = model(X_tv, y_combo). Please don't post code in comments, edit your question to clarify your question.
            – jpp
            Nov 9 at 15:55

















          up vote
          1
          down vote













          Yes. This line doesn't create a new variable:



          "cf_" + str(ind) = cf


          Strings are immutable. You can't "assign a dataframe to a string" either, I'm not even sure what this is attempting to achieve. It's also good practice to avoid global variables.



          Just return your dataframe and assign to an explicit variable name. If you are set on changing your variable name, use a dictionary and dict.pop:



          def model(ind, dep):
          # ...
          cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
          cf.index = models
          cf.columns = models
          # ...
          return cf

          dfs = {}

          dfs['ind'] = model(X_tv, y_combo) # identify dataframe with key 'ind'
          dfs['cf_ind'] = dfs.pop('ind') # rename identifier to 'cf_ind'





          share|improve this answer





















          • Thanks. I have a number of dataframes I want to pull out of the function and save. In the end I have used the global options and then returned then. Then in between each call of the function I have appended the new table.model (X_tv, y_combo, 'just_trans') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff) model(X_tv_cattot, y_combo, 'trans_cattotals') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff)
            – James Adams
            Nov 9 at 15:54












          • I want to pull out of the function and save. That's what this does: dfs['ind'] = model(X_tv, y_combo). Please don't post code in comments, edit your question to clarify your question.
            – jpp
            Nov 9 at 15:55















          up vote
          1
          down vote










          up vote
          1
          down vote









          Yes. This line doesn't create a new variable:



          "cf_" + str(ind) = cf


          Strings are immutable. You can't "assign a dataframe to a string" either, I'm not even sure what this is attempting to achieve. It's also good practice to avoid global variables.



          Just return your dataframe and assign to an explicit variable name. If you are set on changing your variable name, use a dictionary and dict.pop:



          def model(ind, dep):
          # ...
          cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
          cf.index = models
          cf.columns = models
          # ...
          return cf

          dfs = {}

          dfs['ind'] = model(X_tv, y_combo) # identify dataframe with key 'ind'
          dfs['cf_ind'] = dfs.pop('ind') # rename identifier to 'cf_ind'





          share|improve this answer












          Yes. This line doesn't create a new variable:



          "cf_" + str(ind) = cf


          Strings are immutable. You can't "assign a dataframe to a string" either, I'm not even sure what this is attempting to achieve. It's also good practice to avoid global variables.



          Just return your dataframe and assign to an explicit variable name. If you are set on changing your variable name, use a dictionary and dict.pop:



          def model(ind, dep):
          # ...
          cf = pd.DataFrame(confusion_matrix(y_train, y_pred))
          cf.index = models
          cf.columns = models
          # ...
          return cf

          dfs = {}

          dfs['ind'] = model(X_tv, y_combo) # identify dataframe with key 'ind'
          dfs['cf_ind'] = dfs.pop('ind') # rename identifier to 'cf_ind'






          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Nov 9 at 11:13









          jpp

          87.8k195099




          87.8k195099












          • Thanks. I have a number of dataframes I want to pull out of the function and save. In the end I have used the global options and then returned then. Then in between each call of the function I have appended the new table.model (X_tv, y_combo, 'just_trans') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff) model(X_tv_cattot, y_combo, 'trans_cattotals') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff)
            – James Adams
            Nov 9 at 15:54












          • I want to pull out of the function and save. That's what this does: dfs['ind'] = model(X_tv, y_combo). Please don't post code in comments, edit your question to clarify your question.
            – jpp
            Nov 9 at 15:55




















          • Thanks. I have a number of dataframes I want to pull out of the function and save. In the end I have used the global options and then returned then. Then in between each call of the function I have appended the new table.model (X_tv, y_combo, 'just_trans') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff) model(X_tv_cattot, y_combo, 'trans_cattotals') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff)
            – James Adams
            Nov 9 at 15:54












          • I want to pull out of the function and save. That's what this does: dfs['ind'] = model(X_tv, y_combo). Please don't post code in comments, edit your question to clarify your question.
            – jpp
            Nov 9 at 15:55


















          Thanks. I have a number of dataframes I want to pull out of the function and save. In the end I have used the global options and then returned then. Then in between each call of the function I have appended the new table.model (X_tv, y_combo, 'just_trans') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff) model(X_tv_cattot, y_combo, 'trans_cattotals') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff)
          – James Adams
          Nov 9 at 15:54






          Thanks. I have a number of dataframes I want to pull out of the function and save. In the end I have used the global options and then returned then. Then in between each call of the function I have appended the new table.model (X_tv, y_combo, 'just_trans') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff) model(X_tv_cattot, y_combo, 'trans_cattotals') masterCM=masterCM.append(cf) masterCOEFF=masterCOEFF.append(coeff)
          – James Adams
          Nov 9 at 15:54














          I want to pull out of the function and save. That's what this does: dfs['ind'] = model(X_tv, y_combo). Please don't post code in comments, edit your question to clarify your question.
          – jpp
          Nov 9 at 15:55






          I want to pull out of the function and save. That's what this does: dfs['ind'] = model(X_tv, y_combo). Please don't post code in comments, edit your question to clarify your question.
          – jpp
          Nov 9 at 15:55




















          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%2f53224522%2fcreate-a-python-dataframe-using-string-of-variable%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







          這個網誌中的熱門文章

          Xamarin.form Move up view when keyboard appear

          Post-Redirect-Get with Spring WebFlux and Thymeleaf

          Anylogic : not able to use stopDelay()