Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trialJonathan Grieve
Treehouse Moderator 91,253 PointsI'm struggling with Pandas Broadcasting! Has no effect on values
Hello all. :)
I'm struggling with following on, with Python Pandas particularly with Broadcasting. I'm not getting the answers I'm expecting.
I assume from the second half of the note I'm supposed to include the dictionaries on deposits and the balance. And carry on from there.
#do imports
import numpy as np
import pandas as pd
#define data as python dicts
test_balance_data = {
'pasan': 20.00,
'treasure': 20.18,
'ashley': 1.05,
'craig': 42.42,
}
test_deposit_data = {
'pasan': 20,
'treasure': 10,
'ashley': 100,
'craig': 55,
}
balances = pd.Series(test_balance_data)
deposits = pd.Series(test_deposit_data)
print(balances)
Vectorisation gets me the result I'm expecting. But when I try to Broadcast with a scalar value:
#vectorise deposits
balances += deposits
print(balances)
balances + 5
print(balances)
pasan 40.00
treasure 30.18
ashley 101.05
craig 97.42
dtype: float64
It has no affect on the values at all, and I'm not sure why.
As far as I'm aware I'm using the up to date version of Python python 3.6.4
2 Answers
ursaminor
11,271 PointsTry balances += 5
ursaminor
11,271 PointsIn Python, built-in types like int, string, float, are immutable. That's why you have to reassign the variable back to itself after adding to it. (+= means balances = balances + 5
) You can try it out in IDLE:
>>> a = 5
>>> a + 3 // get back 8
8
>>> a // check a again and
5 // it's still 5
Here's an article about it: https://medium.com/@meghamohan/mutable-and-immutable-side-of-python-c2145cf72747
ursaminor
11,271 PointsHmm, reading the instructions, it sounds like it's saying pandas overrides mathematical operations. So maybe there's another answer. My answer is about how Python works, but maybe for this exercise there was another reason why your code didn't work.
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi Schola,
Yes, that seems to work! So it's just like Vectorisation only you use an explicit value rather than a variable. It'd be interesting to know if it's a syntax difference between different versions of Python/Pandas or just a mistake in the notes though there doesn't seem to be an answer out there on the web :) Anything on this Craig Dennis ? Thanks
Craig Dennis
Treehouse TeacherCraig Dennis
Treehouse TeacherHi Jonathan!
The addition operation there is returning a new Series, so you could either store the result in a new variable or use in place addition the +=.
That make sense?
Jonathan Grieve
Treehouse Moderator 91,253 PointsJonathan Grieve
Treehouse Moderator 91,253 PointsHi Craig,
Yes, I'm getting it now. Thanks so much for your input :)
Having caught up and read everyone's responses I went back to using the addition operator as applied in the notes but assigned it to a variable and that got the output I was expecting. I wish my brain was quicker on picking up on that stuff but I understand now.