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
python string dataframe
add a comment |
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
python string dataframe
add a comment |
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
python string dataframe
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
python string dataframe
asked Nov 9 at 11:05
James Adams
821213
821213
add a comment |
add a comment |
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'
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
add a comment |
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'
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
add a comment |
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'
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
add a comment |
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'
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'
answered Nov 9 at 11:13
jpp
84k194897
84k194897
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
add a comment |
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
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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