When I use :
w = y.groupby['A'].size()
It gives me the column values of Column A and then the size of the grouping beside it. Using w ( a Series ), how do I print separately the names of the groupings ? The values of the grouping can be gotten via :
for i in w :
print(i)
But I can't figure out how to get the names.
You can iterate over series using iteritems()
In [100]: for index, val in w.iteritems():
.....: print index, val
.....:
where, index
has column names, and val
value/size
In python 3 the following works fine:
In [31]:
df = pd.DataFrame({"A":["a","b","c","a"]})
w = df.groupby('A').size()
w
Out[31]:
A
a 2
b 1
c 1
dtype: int64
In [61]:
for i,r in w.items():
print(i,r)
a 2
b 1
c 1
As does iterkv
:
In [62]:
for i,r in w.iterkv():
print(i,r)
a 2
b 1
c 1
However, the portable method (will work in python 2 also) is to use iteritems
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With