pandas stack在两列的组合中找到唯一的

时间:2018-03-24 17:32:26

标签: pandas pandas-groupby

我正在尝试在 Field1 Field2 中找到唯一值以及来自 Value1 Value2 <的相应值的总和/ strong>即可。

请在下面找到示例输入和输出。我尝试使用堆栈但无法获得类似的输出。

输入

id  Field1  Value1  Field2  Value2
1     A       1       B       0
1     A       0     
2     A       1       D       1
3     C       1       A       0
4     E       0     
3     A       1       C       1
4     F       1

输出

id  Field   Value
1     A       1
1     B       0     
2     A       1
2     D       1
3     C       2
3     A       1
4     E       0
4     F       1

如果列包含特定值以及其他值,我正在尝试删除行。

例如,在上面的输出中,我会删除带有 A,B 的行(如果它们与其他行一起出现),如果它们看起来是独立的,则会打印出来。请参阅下面的新输出

输出2

id  Field   Value
1     A       1
1     B       0     
2     D       1
3     C       2
4     E       0
4     F       1

1 个答案:

答案 0 :(得分:1)

wide_to_longpublic class MainActivity extends AppCompatActivity { private BottomNavigationView bottomNavigationView; private final String IS_FIRST_USE_FLAG = "IS_FIRST_USE_FLAG"; Semaphore waitUntiAccessTokenIsObtained = new Semaphore(0); private static boolean isFirstUseFlagValue; SharedPreferences prefs; ClientCredentialsStore credentialsStore; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); prefs = this.getSharedPreferences("com.myapp", Context.MODE_PRIVATE); handleFlags(); if(this.isFirstUseFlagValue){ registerClient(); getAccessToken(); try { waitUntiAccessTokenIsObtained.acquire(); } catch (InterruptedException e) { e.printStackTrace(); } } setContentView(R.layout.main_screen); bottomNavigationView = (BottomNavigationView) findViewById(R.id.navigation); bottomNavigationView .setOnNavigationItemSelectedListener (new BottomNavigationView.OnNavigationItemSelectedListener() { @Override public boolean onNavigationItemSelected(@NonNull MenuItem item) { Fragment selectedFragment = null; switch (item.getItemId()) { case R.id.action_item1: selectedFragment = HomeFragment.newInstance(); break; case R.id.action_item2: selectedFragment = CathegoriesListFragment.newInstance(); break; case R.id.action_item3: selectedFragment = TestFragment.newInstance(); break; case R.id.action_item4: selectedFragment = ItemThreeFragment.newInstance(); break; } FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame_layout, selectedFragment); transaction.commit(); return true; } }); //Manually displaying the first fragment - one time only FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); transaction.replace(R.id.frame_layout, HomeFragment.newInstance()); transaction.commit(); //Used to select an item programmatically //bottomNavigationView.getMenu().getItem(1).setChecked(true); } private void handleFlags(){ this.isFirstUseFlagValue = prefs.getBoolean(this.IS_FIRST_USE_FLAG, true); } private void registerClient(){ // TODO client registration on server. String clientNameValue = "clientId"; String clientPasswordValue ="clientSecret"; credentialsStore = new ClientCredentialsStore(getApplicationContext()); credentialsStore.save(clientNameValue, clientPasswordValue); } private void getAccessToken(){ // Obtain token from the server. ClientCredentialsStore credentialsStore = new ClientCredentialsStore(getApplicationContext()); ClientCredentials credentials = credentialsStore.getCredentials(); Call<AccessToken> call = ClientAPI.oAuth2ClientCredentialsGrant(credentials).tokenClient( ClientAccessTokenRequest.from()); call.enqueue(new Callback<AccessToken>() { @Override public void onResponse(Call<AccessToken> call, retrofit2.Response<AccessToken> response) { AccessToken accessToken = response.body(); TokenStore store = new TokenStore(getApplicationContext()); store.save(accessToken); waitUntiAccessTokenIsObtained.release(); waitUntiAccessTokenIsObtained.release(); } @Override public void onFailure(Call<AccessToken> call, Throwable t) { Log.e("MainActivity", "could not retrieve access token", t); } }); } } 一起使用并汇总groupby

sum

替代df = (pd.wide_to_long(df.reset_index(), stubnames=['Field', 'Value'], i='index', j='id') .reset_index(drop=True) .groupby(by=['id', 'Field'], as_index=False)['Value'].sum()) ,但仍不在官方docs中:

lreshape