I want to compare two cloumns in the same dataframe and calculate the sum
this is my code:
sum=0
if df1['sma'] < df1['H+L'] :
sum=sum + df1['H+L']
print (sum)
this is my dataFrame:
df1 = pd.DataFrame({'sma':[1, 5, 2],
'Low':[1,2,3],
'H+L':[10, 20, 0]},
index=pd.date_range('2018-11-30', periods=3)).T
print (df1)
2018-11-30 2018-12-01 2018-12-02
sma 1 5 2
Low 1 2 3
H+L 10 20 0
the output is a sum of all the rows. But always I get this error message
The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I can't understand the issue.
I believe you need filter by boolean indexing
with loc
and use sum
:
df1 = pd.DataFrame({'sma':[1, 5, 2],
'H+L':[10, 20, 0]})
print (df1)
sma H+L
0 1 10
1 5 20
2 2 0
print (df1.loc[df1['sma'] < df1['H+L'],'H+L']))
0 10
1 20
Name: H+L, dtype: float64
out = df1.loc[df1['sma'] < df1['H+L'],'H+L'].sum()
print (out)
30
If working with rows:
df1 = pd.DataFrame({'sma':[1, 5, 2],
'H+L':[10, 20, 0]}).T
print (df1)
0 1 2
sma 1 5 2
H+L 10 20 0
out = df1.loc['H+L', df1.loc['sma'] < df1.loc['H+L']].sum()
print (out)
30
跟随应该起作用;
df1.apply(lambda x: x['H+L'] if x['sma']<x['H+L'] else 0, axis = 1).sum()
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.