Reputation: 4256
feel like this is a really easy question but my brain is fried.
if i have a couple variables that contain pandas series, how can i reference the variable names themselves instead of the contents, when i try to print. Here's what's going on.
first = pd.Series({1:2, 2:4})
second = pd.Series({1:5, 2:7})
all = [first, second]
for i in all:
print i, 'has a sum of', i.sum()
this currently prints the whole dataframe for each variable. how do i end up with an output like :
"first has a sum of 6
second has a sum of 12"
Upvotes: 1
Views: 54
Reputation: 393933
Personally I'd use a dict:
In [15]:
first = pd.Series({1:2, 2:4})
second = pd.Series({1:5, 2:7})
d={}
d['first']=first
d['second']=second
for k,v in d.items():
print(k, 'has a sum of', v.sum())
output:
first has a sum of 6
second has a sum of 12
Also don't use a variable name of all
this shadows the function all
EDIT
If the order matters then you can use an OrderedDict
:
In [20]:
from collections import OrderedDict
first = pd.Series({1:2, 2:4})
second = pd.Series({1:5, 2:7})
d=OrderedDict()
d['first']=first
d['second']=second
for k,v in d.items():
print(k, 'has a sum of', v.sum())
first has a sum of 6
second has a sum of 12
Upvotes: 3