Python 是否应用于非分组数据帧?
尝试实现一个简单的func,随机将一个组标记为Python 是否应用于非分组数据帧?,python,pandas,Python,Pandas,尝试实现一个简单的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.choi
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
功能:
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
在分组数据帧上应用它可以正常工作:
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")
In[149]:df.apply(pickone,group=“a”,out=“c”)
---------------------------------------------------------------------------
TypeError回溯(最近一次调用上次)
pandas/_libs/index.pyx在pandas中。_libs.index.IndexEngine.get_loc(pandas/_libs/index.c:5085)()
pandas/_libs/hashtable\u class\u helper.pxi在pandas中。_libs.hashtable.Int64HashTable.get\u项(pandas/_libs/hashtable.c:13892)()
TypeError:需要一个整数
在处理上述异常期间,发生了另一个异常:
KeyError回溯(最近一次呼叫最后一次)
在()
---->1 df.应用(pickone,group=“a”,out=“c”)
df
是一个数据帧,而df.groupby(…)
是一个DataFrameGroupBy
对象。
这是两种完全不同的方法
df.apply
用于为每行(默认情况下)或每列调用一次函数。
函数需要一个系列(一行或一列)作为其第一个参数
df.groupby(…).apply用于为每个组调用一次函数。
该函数需要一个(子)数据帧作为其第一个参数
要在df
上调用pickone
,请使用
pickone(df, group='a', out='c')
而不是df.apply(pickone,…)
顺便说一下
df[df[group]==p][out] = True
是使用链接索引的赋值。因为,对于某些数据帧,df[df[group]==p]
可能会返回一个新的数据帧,其中包含从df
复制的数据,df[df[group]==p][out]=True
可能会修改新的数据帧,而不是df
本身
因此,使用链式索引的赋值是不正确的。改用df.loc
:
df[out] = False
df.loc[df[group]==p, out] = True
或者,在这种情况下
df[out] = (df[group]==p)
够了。df
是一个数据帧,而df.groupby(…)
是一个DataFrameGroupBy
对象。
这是两种完全不同的方法
df.apply
用于为每行(默认情况下)或每列调用一次函数。
函数需要一个系列(一行或一列)作为其第一个参数
df.groupby(…).apply用于为每个组调用一次函数。
该函数需要一个(子)数据帧作为其第一个参数
要在df
上调用pickone
,请使用
pickone(df, group='a', out='c')
而不是df.apply(pickone,…)
顺便说一下
df[df[group]==p][out] = True
是使用链接索引的赋值。因为,对于某些数据帧,df[df[group]==p]
可能会返回一个新的数据帧,其中包含从df
复制的数据,df[df[group]==p][out]=True
可能会修改新的数据帧,而不是df
本身
因此,使用链式索引的赋值是不正确的。改用df.loc
:
df[out] = False
df.loc[df[group]==p, out] = True
或者,在这种情况下
df[out] = (df[group]==p)
够了