I have a difficulty in calculating “total_sum.” If someone didn’t apply to subject, I expressed N/A. When total_sum is calculated, total_sum refer to Standard field and N/A is excluded. I’m not good at Python, So I don’t know how to calculate “total_sum”
JavaScript
x
6
1
ENG MATH ART COM subject_sum total_sum
2
Standard 10 10 5 5
3
A 10 N/A N/A 1 11 15
4
B N/A 5 3 5 13 20
5
C 3 3 2 2 10 30
6
Advertisement
Answer
Suppose this dataframe is the same as yours (with index of strings)
JavaScript
1
16
16
1
dataframe = {'index' :['Standard', 'A', 'B', 'C'],
2
'ENG' : [10, 10, np.nan, 3],
3
'MATH' : [10, np.nan, 5, 3],
4
'ART' : [5, np.nan, 3, 2],
5
'COM' : [5, 1, 5, 2]}
6
df = pd.DataFrame(dataframe).set_index('index').rename_axis(None)
7
df['subject_sum'] = df.sum(axis=1)
8
df
9
10
11
ENG MATH ART COM subject_sum
12
Standard 10.0 10.0 5.0 5 30.0
13
A 10.0 NaN NaN 1 11.0
14
B NaN 5.0 3.0 5 13.0
15
C 3.0 3.0 2.0 2 10.0
16
then you can apply .dot()
of every .notna()
subject values to the values of Standard
JavaScript
1
4
1
standard = df.loc['Standard', ['ENG', 'MATH', 'ART', 'COM']]
2
df['total_sum'] = df[['ENG', 'MATH', 'ART', 'COM']].notna().dot(standard)
3
df
4
result :
JavaScript
1
6
1
ENG MATH ART COM subject_sum total_sum
2
Standard 10.0 10.0 5.0 5 30.0 30.0
3
A 10.0 NaN NaN 1 11.0 15.0
4
B NaN 5.0 3.0 5 13.0 20.0
5
C 3.0 3.0 2.0 2 10.0 30.0
6