OiO.lk Community platform!

Oio.lk is an excellent forum for developers, providing a wide range of resources, discussions, and support for those in the developer community. Join oio.lk today to connect with like-minded professionals, share insights, and stay updated on the latest trends and technologies in the development field.
  You need to log in or register to access the solved answers to this problem.
  • You have reached the maximum number of guest views allowed
  • Please register below to remove this limitation

Sum rows from a column based on a condition and output them in a new column

  • Thread starter Thread starter mxplk
  • Start date Start date
M

mxplk

Guest
In my dataframe I want to sum certain rows in a column and output them in a new column 'UE_more_days'

is

Code:
    ATEXT   BEGUZ_UE   UE_more_days
0             11.00            0.0
1     CT      23.00            Nan
2     RT      33.00           46.0
3             15.00            0.0
3             15.00            0.0
4             12.75            0.0
5             19.75            0.0
6             14.75            0.0
7     CT      23.00           29.5
8     CT      24.00           46.0
9     CT      24.00           48.0
10    RT      33.00           48.0
11            15.00            0.0
12
etc

should be

Code:
    ATEXT   BEGUZ_UE   UE_more_days
0
1     CT      23.00
2     RT      33.00           56.0
3             15.00
4             12.75
5             19.75
6             14.75
7     CT      23.00
8     CT      24.00
9     CT      24.00
10    RT      33.00          104.0
11            15.00
12
etc

my last try

Code:
bedd2 = [(df['ATEXT'] != ''),]
result2 = [(df.iloc[0:]['BEGUZ_UE'].astype(float).reset_index(drop=True) +
df.iloc[1:]['BEGUZ_UE'].astype(float)).round(decimals=2).shift(1)]
df['min_UE_mehr_Tage'] = np.select(bedd2, result2)

How can I sum rows from a column based on a condition and output them in a new column?
<p>In my dataframe I want to sum certain rows in a column and output them in a new column 'UE_more_days'</p>
<p>is</p>
<pre><code> ATEXT BEGUZ_UE UE_more_days
0 11.00 0.0
1 CT 23.00 Nan
2 RT 33.00 46.0
3 15.00 0.0
3 15.00 0.0
4 12.75 0.0
5 19.75 0.0
6 14.75 0.0
7 CT 23.00 29.5
8 CT 24.00 46.0
9 CT 24.00 48.0
10 RT 33.00 48.0
11 15.00 0.0
12
etc
</code></pre>
<p>should be</p>
<pre><code> ATEXT BEGUZ_UE UE_more_days
0
1 CT 23.00
2 RT 33.00 56.0
3 15.00
4 12.75
5 19.75
6 14.75
7 CT 23.00
8 CT 24.00
9 CT 24.00
10 RT 33.00 104.0
11 15.00
12
etc
</code></pre>
<p>my last try</p>
<pre><code>bedd2 = [(df['ATEXT'] != ''),]
result2 = [(df.iloc[0:]['BEGUZ_UE'].astype(float).reset_index(drop=True) +
df.iloc[1:]['BEGUZ_UE'].astype(float)).round(decimals=2).shift(1)]
df['min_UE_mehr_Tage'] = np.select(bedd2, result2)
</code></pre>
<p>How can I sum rows from a column based on a condition and output them in a new column?</p>
 

Latest posts

M
Replies
0
Views
1
MusicLovingIndianGirl
M
Q
Replies
0
Views
1
quora question
Q
Top