1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Dataframeで条件満足する行のみに対し合計値を算出する

Last updated at Posted at 2021-12-19

A列が1, B列が2 である行のC列の合計値

test.py
import pandas as pd

df1 = pd.DataFrame([[1, 1, 12],
                    [1, 2, 11],
                    [1, 1, 18],
                    [2, 2, 19],
                    [2, 1, 10],
                    [2, 2, 14],
                    [1, 1, 15],
                    [1, 2, 17],
                    [1, 1, 19]],
                   columns=['A', 'B', 'C'])

sum_of_c = df1.loc[(df1['A'] == 1) & (df1['B'] == 2), 'C'].sum()

print('sum={}'.format(sum_of_c))

出力結果

sum=28
1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?