python - Pandas dataframe - running sum within cluster -


मेरे पास

  x cluster_id 0 1 1 1 3 1 2 2 2 3 5 2 4 4 3  

मैं उत्पन्न करना चाहता हूं

  x cluster_id s 0 1 1 1 1 3 1 4 2 2 2 3 3 5 2 7 4 4 3 4  

अर्थात् s चल रहा है x , लेकिन क्लस्टर आईडी में परिवर्तन होने पर इसे रीसेट हो जाता है। यह कैसे हासिल किया जाता है?

वैकल्पिक रूप से, यदि यह आसान है, तो यह ठीक हो सकता है

  x cluster_id 0 1 1 4 1 3 1 4 2 2 2 7 3 5 2 7 4 4 3 4  

अर्थात् s के लिए समान क्लस्टर के सभी मान एक जैसे होते हैं, और क्लस्टर में कुल राशि के अनुरूप होते हैं।

इसके अतिरिक्त, मैं इसे नमस्कार करना चाहता हूं ताकि मैं अंतिम पंक्ति प्रत्येक क्लस्टर का:

  x cluster_id s 1 3 1 4 3 5 2 7 4 4 3 4  

(ध्यान दें कि सभी क्लस्टर आईडी अलग हैं) । आप यह कैसे कर सकते हैं?

आप .cumsum () का उपयोग कर चलने वाली योग प्राप्त कर सकते हैं < / Code> के साथ .groupby ()

  & gt; & gt; & gt; Df x क्लस्टर_आईडी 0 1 1 1 3 1 2 2 2 3 5 2 4 4 3 & gt; & gt; & gt; Df ['s'] = df.groupby ('cluster_id')। Cumsum () & gt; & gt; & gt; Df x cluster_id 0 1 1 1 1 3 1 4 2 2 2 2 3 5 2 7 4 4 3 4  

फिर प्रत्येक cluster_id :

  & gt; & gt; & gt; Df.groupby ('cluster_id')। पिछले ()। Reset_index () cluster_id x s 0 1 3 4 1 2 5 7 2 3 4 4  

Comments

Popular posts from this blog

apache - 504 Gateway Time-out The server didn't respond in time. How to fix it? -

c# - .net WebSocket: CloseOutputAsync vs CloseAsync -

c++ - How to properly scale qgroupbox title with stylesheet for high resolution display? -