Last active
May 31, 2018 11:05
-
-
Save kyoro1/b6343c2d65480b7de301cc2fbe21cb2c to your computer and use it in GitHub Desktop.
pandasで、行 or 列内に欠損値がある時に、無視して加算・無視せず加算する方法 ref: https://qiita.com/kyoro1/items/ce89d6d444eec2b26512
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df | |
# C1 C2 C3 | |
#0 1.0 2 NaN | |
#1 NaN 1 NaN | |
#2 NaN 3 1.0 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df[['C1']].sum(skipna=True) | |
#C1 1.0 | |
#dtype: float64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df[['C2']].sum(skipna=True) | |
#C2 6 | |
#dtype: int64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
df[['C1']].sum(skipna=False) | |
#C1 NaN | |
#dtype: float64 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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