相关疑难解决方法(0)

对组对象应用vs转换

考虑以下数据帧:

     A      B         C         D
0  foo    one  0.162003  0.087469
1  bar    one -1.156319 -1.526272
2  foo    two  0.833892 -1.666304
3  bar  three -2.026673 -0.322057
4  foo    two  0.411452 -0.954371
5  bar    two  0.765878 -0.095968
6  foo    one -0.654890  0.678091
7  foo  three -1.789842 -1.130922
Run Code Online (Sandbox Code Playgroud)

以下命令有效:

> df.groupby('A').apply(lambda x: (x['C'] - x['D']))
> df.groupby('A').apply(lambda x: (x['C'] - x['D']).mean())
Run Code Online (Sandbox Code Playgroud)

但没有以下工作:

> df.groupby('A').transform(lambda x: (x['C'] - x['D']))
ValueError: could not broadcast input array from shape (5) into shape (5,3)

> df.groupby('A').transform(lambda x: …
Run Code Online (Sandbox Code Playgroud)

python pandas

142
推荐指数
4
解决办法
7万
查看次数

标签 统计

pandas ×1

python ×1