How can I troubleshoot a function that returns an empty dataframe?











up vote
-2
down vote

favorite












I invented a little game that would bring up pictures of American legislators and ask the user to choose one of them only based on their looks (yes, it is an artistic comment on my generation's political engagement). After a few matches, the game would bring up an objection to the choice, ultimately based on some issues the user has indicated they care about. To test the game and its subsequent analytics, I wrote a function to return user data, but it is returning only an empty dataframe. Can anyone spot something incorrect with it? I realize the end of the function is wordy, but I was trying a "brute force" approach after my more elegant approaches didn't work.



Note: there is a dataframe called zipcodes that includes state, zipcode, and congressional district. There is also a dataframe claled legislators that includes information about legislators in the current Congress.



def play_game():

#Create player
#These probabilities are made up...
#...but it doesn't matter since I am just filling a DB as a
placeholder for real data
user_id = str(uuid.uuid4()) #generate user_id
gender = np.random.choice(genders, p=[0.495, 0.495, 0.01])
age = np.random.choice(range(13, 75))
cares_about = np.random.choice(cares,
size=np.random.choice(range(len(cares))))
user_party = np.random.choice(parties, p=[0.35, 0.35, 0.15, 0.15])
zip_state_dist = zipcodes.sample(1, replace=True)
zip_code = zip_state_dist.iloc[0]['Zipcode']
state = zip_state_dist.iloc[0]['State']
congressional_district = zip_state_dist.iloc[0]['Congressional District']

#Getting the user's legislators
#sr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'senior')].iloc[0]['name']
#jr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'junior')].iloc[0]['name']
#representative = legislators[
#(legislators['state'] == state) &
#(legislators['district'] == congressional_district)].iloc[0]['name']

#create df for player's id
user_id_columns = ['age', 'gender', 'cares', 'zipcode', 'state', 'congressional_district','party']
user_id_info = pd.DataFrame(columns=user_id_columns)
user_id_info['user_id'] = user_id
user_id_info['age'] = age
user_id_info['gender'] = gender
user_id_info['cares'] = cares_about
user_id_info['zipcode'] = zip_code
user_id_info['congressional_district'] = congressional_district
user_id_info['party'] = user_party



#an objection is raised and answered with a probability distribution
def objection(winner, picks):
legislator_slice = legislators[legislators['name'] == winner].iloc[0]
if legislator_slice['party'] == user_party: #if parties are same, 75% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.75, 0.25])
elif legislator_slice['party'] != user_party: #if parties are different, 25% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.25, 0.75])
return winner_after_objection

#make DataFrame for results
user_id_results = pd.DataFrame(columns=['user_id',
'winner1', 'loser1',
'winner2', 'loser2',
'winner3_before', 'loser3_before',
'winner3_after', 'loser3_after',
'winner4_before', 'loser4_before',
'winner4_after', 'loser4_after',
'winner5_before', 'loser5_before',
'winner5_after', 'loser5_after',])
user_id_results['user_id'] = user_id
# show 2 choices without objections
for i in range(2):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]

user_id_results['winner1'] = winner
user_id_results['loser1'] = loser
user_id_results['winner2'] = winner
user_id_results['loser2'] = loser



#show 3 choices with objections
for i in range(2,5):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]
new_winner = objection(winner, picks)
new_loser = picks[1-picks.index(new_winner)]

if i == 2:
user_id_results['winner3_before'] = winner
user_id_results['loser3_before'] = loser
user_id_results['winner3_after'] = new_winner
user_id_results['loser3_after'] = new_loser
elif i == 3:
user_id_results['winner4_before'] = winner
user_id_results['loser4_before'] = loser
user_id_results['winner4_after'] = new_winner
user_id_results['loser4_after'] = new_loser
elif i == 4:
user_id_results['winner5_before'] = winner
user_id_results['loser5_before'] = loser
user_id_results['winner5_after'] = new_winner
user_id_results['loser5_after'] = new_loser

return user_id_results









share|improve this question






















  • I dont really have the energy to run through the full code - but the simple question is to see what debugging you have done already. Have you checked the user_id_results after it was declared? As you arent using an empty declarator, there should be some content in that position already. Check with print(user_id_results.head())
    – Henry
    Nov 11 at 22:31










  • Yeah, just bothering to print variables...
    – barny
    Nov 11 at 23:17










  • Thanks so much. I've figured out the answer, which is that I had to encase each entry in a list.
    – Mac McCarthy
    Nov 14 at 22:56















up vote
-2
down vote

favorite












I invented a little game that would bring up pictures of American legislators and ask the user to choose one of them only based on their looks (yes, it is an artistic comment on my generation's political engagement). After a few matches, the game would bring up an objection to the choice, ultimately based on some issues the user has indicated they care about. To test the game and its subsequent analytics, I wrote a function to return user data, but it is returning only an empty dataframe. Can anyone spot something incorrect with it? I realize the end of the function is wordy, but I was trying a "brute force" approach after my more elegant approaches didn't work.



Note: there is a dataframe called zipcodes that includes state, zipcode, and congressional district. There is also a dataframe claled legislators that includes information about legislators in the current Congress.



def play_game():

#Create player
#These probabilities are made up...
#...but it doesn't matter since I am just filling a DB as a
placeholder for real data
user_id = str(uuid.uuid4()) #generate user_id
gender = np.random.choice(genders, p=[0.495, 0.495, 0.01])
age = np.random.choice(range(13, 75))
cares_about = np.random.choice(cares,
size=np.random.choice(range(len(cares))))
user_party = np.random.choice(parties, p=[0.35, 0.35, 0.15, 0.15])
zip_state_dist = zipcodes.sample(1, replace=True)
zip_code = zip_state_dist.iloc[0]['Zipcode']
state = zip_state_dist.iloc[0]['State']
congressional_district = zip_state_dist.iloc[0]['Congressional District']

#Getting the user's legislators
#sr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'senior')].iloc[0]['name']
#jr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'junior')].iloc[0]['name']
#representative = legislators[
#(legislators['state'] == state) &
#(legislators['district'] == congressional_district)].iloc[0]['name']

#create df for player's id
user_id_columns = ['age', 'gender', 'cares', 'zipcode', 'state', 'congressional_district','party']
user_id_info = pd.DataFrame(columns=user_id_columns)
user_id_info['user_id'] = user_id
user_id_info['age'] = age
user_id_info['gender'] = gender
user_id_info['cares'] = cares_about
user_id_info['zipcode'] = zip_code
user_id_info['congressional_district'] = congressional_district
user_id_info['party'] = user_party



#an objection is raised and answered with a probability distribution
def objection(winner, picks):
legislator_slice = legislators[legislators['name'] == winner].iloc[0]
if legislator_slice['party'] == user_party: #if parties are same, 75% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.75, 0.25])
elif legislator_slice['party'] != user_party: #if parties are different, 25% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.25, 0.75])
return winner_after_objection

#make DataFrame for results
user_id_results = pd.DataFrame(columns=['user_id',
'winner1', 'loser1',
'winner2', 'loser2',
'winner3_before', 'loser3_before',
'winner3_after', 'loser3_after',
'winner4_before', 'loser4_before',
'winner4_after', 'loser4_after',
'winner5_before', 'loser5_before',
'winner5_after', 'loser5_after',])
user_id_results['user_id'] = user_id
# show 2 choices without objections
for i in range(2):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]

user_id_results['winner1'] = winner
user_id_results['loser1'] = loser
user_id_results['winner2'] = winner
user_id_results['loser2'] = loser



#show 3 choices with objections
for i in range(2,5):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]
new_winner = objection(winner, picks)
new_loser = picks[1-picks.index(new_winner)]

if i == 2:
user_id_results['winner3_before'] = winner
user_id_results['loser3_before'] = loser
user_id_results['winner3_after'] = new_winner
user_id_results['loser3_after'] = new_loser
elif i == 3:
user_id_results['winner4_before'] = winner
user_id_results['loser4_before'] = loser
user_id_results['winner4_after'] = new_winner
user_id_results['loser4_after'] = new_loser
elif i == 4:
user_id_results['winner5_before'] = winner
user_id_results['loser5_before'] = loser
user_id_results['winner5_after'] = new_winner
user_id_results['loser5_after'] = new_loser

return user_id_results









share|improve this question






















  • I dont really have the energy to run through the full code - but the simple question is to see what debugging you have done already. Have you checked the user_id_results after it was declared? As you arent using an empty declarator, there should be some content in that position already. Check with print(user_id_results.head())
    – Henry
    Nov 11 at 22:31










  • Yeah, just bothering to print variables...
    – barny
    Nov 11 at 23:17










  • Thanks so much. I've figured out the answer, which is that I had to encase each entry in a list.
    – Mac McCarthy
    Nov 14 at 22:56













up vote
-2
down vote

favorite









up vote
-2
down vote

favorite











I invented a little game that would bring up pictures of American legislators and ask the user to choose one of them only based on their looks (yes, it is an artistic comment on my generation's political engagement). After a few matches, the game would bring up an objection to the choice, ultimately based on some issues the user has indicated they care about. To test the game and its subsequent analytics, I wrote a function to return user data, but it is returning only an empty dataframe. Can anyone spot something incorrect with it? I realize the end of the function is wordy, but I was trying a "brute force" approach after my more elegant approaches didn't work.



Note: there is a dataframe called zipcodes that includes state, zipcode, and congressional district. There is also a dataframe claled legislators that includes information about legislators in the current Congress.



def play_game():

#Create player
#These probabilities are made up...
#...but it doesn't matter since I am just filling a DB as a
placeholder for real data
user_id = str(uuid.uuid4()) #generate user_id
gender = np.random.choice(genders, p=[0.495, 0.495, 0.01])
age = np.random.choice(range(13, 75))
cares_about = np.random.choice(cares,
size=np.random.choice(range(len(cares))))
user_party = np.random.choice(parties, p=[0.35, 0.35, 0.15, 0.15])
zip_state_dist = zipcodes.sample(1, replace=True)
zip_code = zip_state_dist.iloc[0]['Zipcode']
state = zip_state_dist.iloc[0]['State']
congressional_district = zip_state_dist.iloc[0]['Congressional District']

#Getting the user's legislators
#sr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'senior')].iloc[0]['name']
#jr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'junior')].iloc[0]['name']
#representative = legislators[
#(legislators['state'] == state) &
#(legislators['district'] == congressional_district)].iloc[0]['name']

#create df for player's id
user_id_columns = ['age', 'gender', 'cares', 'zipcode', 'state', 'congressional_district','party']
user_id_info = pd.DataFrame(columns=user_id_columns)
user_id_info['user_id'] = user_id
user_id_info['age'] = age
user_id_info['gender'] = gender
user_id_info['cares'] = cares_about
user_id_info['zipcode'] = zip_code
user_id_info['congressional_district'] = congressional_district
user_id_info['party'] = user_party



#an objection is raised and answered with a probability distribution
def objection(winner, picks):
legislator_slice = legislators[legislators['name'] == winner].iloc[0]
if legislator_slice['party'] == user_party: #if parties are same, 75% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.75, 0.25])
elif legislator_slice['party'] != user_party: #if parties are different, 25% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.25, 0.75])
return winner_after_objection

#make DataFrame for results
user_id_results = pd.DataFrame(columns=['user_id',
'winner1', 'loser1',
'winner2', 'loser2',
'winner3_before', 'loser3_before',
'winner3_after', 'loser3_after',
'winner4_before', 'loser4_before',
'winner4_after', 'loser4_after',
'winner5_before', 'loser5_before',
'winner5_after', 'loser5_after',])
user_id_results['user_id'] = user_id
# show 2 choices without objections
for i in range(2):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]

user_id_results['winner1'] = winner
user_id_results['loser1'] = loser
user_id_results['winner2'] = winner
user_id_results['loser2'] = loser



#show 3 choices with objections
for i in range(2,5):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]
new_winner = objection(winner, picks)
new_loser = picks[1-picks.index(new_winner)]

if i == 2:
user_id_results['winner3_before'] = winner
user_id_results['loser3_before'] = loser
user_id_results['winner3_after'] = new_winner
user_id_results['loser3_after'] = new_loser
elif i == 3:
user_id_results['winner4_before'] = winner
user_id_results['loser4_before'] = loser
user_id_results['winner4_after'] = new_winner
user_id_results['loser4_after'] = new_loser
elif i == 4:
user_id_results['winner5_before'] = winner
user_id_results['loser5_before'] = loser
user_id_results['winner5_after'] = new_winner
user_id_results['loser5_after'] = new_loser

return user_id_results









share|improve this question













I invented a little game that would bring up pictures of American legislators and ask the user to choose one of them only based on their looks (yes, it is an artistic comment on my generation's political engagement). After a few matches, the game would bring up an objection to the choice, ultimately based on some issues the user has indicated they care about. To test the game and its subsequent analytics, I wrote a function to return user data, but it is returning only an empty dataframe. Can anyone spot something incorrect with it? I realize the end of the function is wordy, but I was trying a "brute force" approach after my more elegant approaches didn't work.



Note: there is a dataframe called zipcodes that includes state, zipcode, and congressional district. There is also a dataframe claled legislators that includes information about legislators in the current Congress.



def play_game():

#Create player
#These probabilities are made up...
#...but it doesn't matter since I am just filling a DB as a
placeholder for real data
user_id = str(uuid.uuid4()) #generate user_id
gender = np.random.choice(genders, p=[0.495, 0.495, 0.01])
age = np.random.choice(range(13, 75))
cares_about = np.random.choice(cares,
size=np.random.choice(range(len(cares))))
user_party = np.random.choice(parties, p=[0.35, 0.35, 0.15, 0.15])
zip_state_dist = zipcodes.sample(1, replace=True)
zip_code = zip_state_dist.iloc[0]['Zipcode']
state = zip_state_dist.iloc[0]['State']
congressional_district = zip_state_dist.iloc[0]['Congressional District']

#Getting the user's legislators
#sr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'senior')].iloc[0]['name']
#jr_senator = legislators[
#(legislators['state'] == state) &
#(legislators['state_rank'] == 'junior')].iloc[0]['name']
#representative = legislators[
#(legislators['state'] == state) &
#(legislators['district'] == congressional_district)].iloc[0]['name']

#create df for player's id
user_id_columns = ['age', 'gender', 'cares', 'zipcode', 'state', 'congressional_district','party']
user_id_info = pd.DataFrame(columns=user_id_columns)
user_id_info['user_id'] = user_id
user_id_info['age'] = age
user_id_info['gender'] = gender
user_id_info['cares'] = cares_about
user_id_info['zipcode'] = zip_code
user_id_info['congressional_district'] = congressional_district
user_id_info['party'] = user_party



#an objection is raised and answered with a probability distribution
def objection(winner, picks):
legislator_slice = legislators[legislators['name'] == winner].iloc[0]
if legislator_slice['party'] == user_party: #if parties are same, 75% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.75, 0.25])
elif legislator_slice['party'] != user_party: #if parties are different, 25% chance that winner will stay
winner_after_objection = np.random.choice([winner, picks[1-picks.index(winner)]],
p = [0.25, 0.75])
return winner_after_objection

#make DataFrame for results
user_id_results = pd.DataFrame(columns=['user_id',
'winner1', 'loser1',
'winner2', 'loser2',
'winner3_before', 'loser3_before',
'winner3_after', 'loser3_after',
'winner4_before', 'loser4_before',
'winner4_after', 'loser4_after',
'winner5_before', 'loser5_before',
'winner5_after', 'loser5_after',])
user_id_results['user_id'] = user_id
# show 2 choices without objections
for i in range(2):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]

user_id_results['winner1'] = winner
user_id_results['loser1'] = loser
user_id_results['winner2'] = winner
user_id_results['loser2'] = loser



#show 3 choices with objections
for i in range(2,5):
picks = legislators.sample(2, replace=True)
picks = [picks.iloc[0]['name'], picks.iloc[1]['name']]
winner = np.random.choice(picks)
loser = picks[1-picks.index(winner)]
new_winner = objection(winner, picks)
new_loser = picks[1-picks.index(new_winner)]

if i == 2:
user_id_results['winner3_before'] = winner
user_id_results['loser3_before'] = loser
user_id_results['winner3_after'] = new_winner
user_id_results['loser3_after'] = new_loser
elif i == 3:
user_id_results['winner4_before'] = winner
user_id_results['loser4_before'] = loser
user_id_results['winner4_after'] = new_winner
user_id_results['loser4_after'] = new_loser
elif i == 4:
user_id_results['winner5_before'] = winner
user_id_results['loser5_before'] = loser
user_id_results['winner5_after'] = new_winner
user_id_results['loser5_after'] = new_loser

return user_id_results






python function dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 11 at 22:28









Mac McCarthy

1




1












  • I dont really have the energy to run through the full code - but the simple question is to see what debugging you have done already. Have you checked the user_id_results after it was declared? As you arent using an empty declarator, there should be some content in that position already. Check with print(user_id_results.head())
    – Henry
    Nov 11 at 22:31










  • Yeah, just bothering to print variables...
    – barny
    Nov 11 at 23:17










  • Thanks so much. I've figured out the answer, which is that I had to encase each entry in a list.
    – Mac McCarthy
    Nov 14 at 22:56


















  • I dont really have the energy to run through the full code - but the simple question is to see what debugging you have done already. Have you checked the user_id_results after it was declared? As you arent using an empty declarator, there should be some content in that position already. Check with print(user_id_results.head())
    – Henry
    Nov 11 at 22:31










  • Yeah, just bothering to print variables...
    – barny
    Nov 11 at 23:17










  • Thanks so much. I've figured out the answer, which is that I had to encase each entry in a list.
    – Mac McCarthy
    Nov 14 at 22:56
















I dont really have the energy to run through the full code - but the simple question is to see what debugging you have done already. Have you checked the user_id_results after it was declared? As you arent using an empty declarator, there should be some content in that position already. Check with print(user_id_results.head())
– Henry
Nov 11 at 22:31




I dont really have the energy to run through the full code - but the simple question is to see what debugging you have done already. Have you checked the user_id_results after it was declared? As you arent using an empty declarator, there should be some content in that position already. Check with print(user_id_results.head())
– Henry
Nov 11 at 22:31












Yeah, just bothering to print variables...
– barny
Nov 11 at 23:17




Yeah, just bothering to print variables...
– barny
Nov 11 at 23:17












Thanks so much. I've figured out the answer, which is that I had to encase each entry in a list.
– Mac McCarthy
Nov 14 at 22:56




Thanks so much. I've figured out the answer, which is that I had to encase each entry in a list.
– Mac McCarthy
Nov 14 at 22:56

















active

oldest

votes











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%2f53253886%2fhow-can-i-troubleshoot-a-function-that-returns-an-empty-dataframe%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown






























active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes
















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%2f53253886%2fhow-can-i-troubleshoot-a-function-that-returns-an-empty-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

Xamarin.iOS Cant Deploy on Iphone

Glorious Revolution

Dulmage-Mendelsohn matrix decomposition in Python