Is it possible to use a dict to group on elements of a column?
For example:
In [3]: df = pd.DataFrame({\'A\' : [\'one\', \'one\', \'two\', \'three\'
You can group with a dictionary, but (as with any group by operation) you need to set the index column first.
grouped = df.set_index("A").groupby(d)
list(grouped)
# [('End', B
# A
# three -1.550727
# three 1.048730
#
# [2 rows x 1 columns]), ('Start', B
# A
# one -1.552152
# one -2.018647
# two -0.968068
# two 0.449016
# two -0.374453
# one 0.116770
#
# [6 rows x 1 columns])]
From the docs, the dict has to map from labels to group names, so this will work if you put 'A'
into the index:
grouped2 = df.set_index('A').groupby(d)
for group_name, data in grouped2:
print group_name
print '---------'
print data
# Output:
End
---------
B
A
three -1.234795
three 0.239209
Start
---------
B
A
one -1.924156
one 0.506046
two -1.681980
two 0.605248
two -0.861364
one 0.800431
Column names and row indices are both labels, whereas before you put 'A'
into the index, the elements of 'A'
are values.
If you have other info in the index that makes doing a set_index()
tricky, you can just create a grouping column with map()
:
df['group'] = df['A'].map(d)
grouped3 = df.groupby('group')