应用groupby的函数

时间:2016-10-15 18:14:48

标签: python-2.7 pandas

我有很多想要应用函数的数据帧。

我的数据框看起来像这样:

Year  ID   Pressure
1984  1    0.2
1985  2    0.5
1986  3    0.7

我在尝试:

def f(x):
   return x.groupby(['ID']).Pressure.mean().to_frame().reset_index()

#apply the function to dataframes
df.apply(f)
df2.apply(f)

但这会返回:

 KeyError: ('ID', u'occurred at index Year')

没有功能我可以做我想做的事情:

df=df.groupby(['ID']).Pressure.mean().to_frame().reset_index()

1 个答案:

答案 0 :(得分:1)

当您希望onOptionsItemSelected() @RunWith(RobolectricGradleTestRunner.class) @Config(constants = BuildConfig.class , sdk = 21) public class AboutTheAppFragmentTest { private Context context; private YourActivity activity; private ActivityController<YourActivity> controller; @Before public void setUp() { context = RuntimeEnvironment.application.getApplicationContext(); controller = Robolectric.buildActivity(YourActivity.class); activity = controller .create() .start() .resume() .visible() .get(); } @Test public void testMenuOptionsSelected(){ MenuItem menuItem = new RoboMenuItem(R.id.your_menu_id); activity.onOptionsItemSelected(menuItem); //write your test assertion } @After public void tearDown() { controller.pause().stop().destroy(); controller = null; context = null; } 数据帧的每个值时,都会使用

apply。因为你只想对整个df应用一些东西,你应该这样做:

apply