試圖實現一個簡單的FUNC標誌着一個組作爲True
,隨機適用於非分組數據框?
數據框:
In [145]: df = pd.DataFrame({'a': [1,1,1,2,2], 'b': [3,3,3,3,3]})
In [146]: df
Out[146]:
a b
0 1 3
1 1 3
2 1 3
3 2 3
4 2 3
功能:
def pickone(df, group, out):
u = df[group].unique()
p = np.random.choice(u, 1)[0]
df[out] = False
df[df[group]==p][out] = True
return df
應用它適用於分組罰款數據框:
In [148]: df.groupby(['b']).apply(pickone, group="a", out="c")
Out[148]:
a b c
0 1 3 True
1 1 3 True
2 1 3 True
3 2 3 False
4 2 3 False
但不能在非分組DFS:
In [149]: df.apply(pickone, group="a", out="c")
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
pandas/_libs/index.pyx in pandas._libs.index.IndexEngine.get_loc (pandas/_libs/index.c:5085)()
pandas/_libs/hashtable_class_helper.pxi in pandas._libs.hashtable.Int64HashTable.get_item (pandas/_libs/hashtable.c:13892)()
TypeError: an integer is required
During handling of the above exception, another exception occurred:
KeyError Traceback (most recent call last)
<ipython-input-149-86c0d6e0e423> in <module>()
----> 1 df.apply(pickone, group="a", out="c")