は、ここでデータフレーム内の所与の列内の指定されたグループに対応するインデックスを返す最小、幾分ロバスト関数の:
# create some data
d = pd.DataFrame({'sex': ['male', 'male', 'female', 'male', 'female', 'female', 'male'], 'age': [23, 24, 20, 32, 45, 43, 32]})
# returns a dictionary with group names as keys and indices corresponding
# to those groups as values (can just use `list` or `set` to avoid pandas indexes
def get_indices(df, col):
return {group: df[df[col] == group].index for group in set(df[col])}
# test it out
get_indices(d, 'sex')
Out[178]:
{'female': Int64Index([2, 4, 5], dtype='int64'),
'male': Int64Index([0, 1, 3, 6], dtype='int64')}
を考えてみましょう – EdChum