Skip to content

Instantly share code, notes, and snippets.

@kyoro1
Last active May 31, 2018 11:05
Show Gist options
  • Save kyoro1/b6343c2d65480b7de301cc2fbe21cb2c to your computer and use it in GitHub Desktop.
Save kyoro1/b6343c2d65480b7de301cc2fbe21cb2c to your computer and use it in GitHub Desktop.
pandasで、行 or 列内に欠損値がある時に、無視して加算・無視せず加算する方法 ref: https://qiita.com/kyoro1/items/ce89d6d444eec2b26512
import pandas as pd
import numpy as np
df = pd.DataFrame({'C1': [1, np.nan, np.nan],
'C2': [2, 1, 3],
'C3': [np.nan, np.nan, 1]})
df
# C1 C2 C3
#0 1.0 2 NaN
#1 NaN 1 NaN
#2 NaN 3 1.0
df[['C1']].sum(skipna=True)
#C1 1.0
#dtype: float64
df[['C2']].sum(skipna=True)
#C2 6
#dtype: int64
df[['C1']].sum(skipna=False)
#C1 NaN
#dtype: float64
df[['C2']].sum(skipna=False)
#C2 6
#dtype: int64
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment