[PYTHON] Use Mean in DataFrame

If you use Mean on a DataFrame that contains a value, you can get the average DataFrame. At that time, check what happens when the target item contains NULL.


import pandas as pd

df_ExistNone = pd.DataFrame({'a': [1, 2, 1,None, 3],
                   'b': [0.4, 1.1,None, 0.1, 0.8],
                   'c': ['X', 'Y',None, 'X', 'Z'],
                   'd': ['3',None, '5', '2', '1'],
                   'e': [True,None, True, False, True]})

df = pd.DataFrame({'a': [1, 2, 1, 3],
                   'b': [0.4, 1.1, 0.1, 0.8],
                   'c': ['X', 'Y', 'X', 'Z'],
                   'd': ['3', '5', '2', '1'],
                   'e': [True, True, False, True]})

df_0 = pd.DataFrame({'a': [1, 2, 1,0, 3],
                   'b': [0.4, 1.1,0, 0.1, 0.8],
                   'c': ['X', 'Y',None, 'X', 'Z'],
                   'd': ['3','0', '5', '2', '1'],
                   'e': [True,None, True, False, True]})

print(df)
print(df_ExistNone)
print(df_0)

print("-------------------")

print(df.mean())
print(df_ExistNone.mean())
print(df_0.mean())

result

   a    b  c  d      e
0  1  0.4  X  3   True
1  2  1.1  Y  5   True
2  1  0.1  X  2  False
3  3  0.8  Z  1   True
     a    b     c     d      e
0  1.0  0.4     X     3   True
1  2.0  1.1     Y  None   None
2  1.0  NaN  None     5   True
3  NaN  0.1     X     2  False
4  3.0  0.8     Z     1   True
   a    b     c  d      e
0  1  0.4     X  3   True
1  2  1.1     Y  0   None
2  1  0.0  None  5   True
3  0  0.1     X  2  False
4  3  0.8     Z  1   True

-------------------
a      1.75
b      0.60
d    880.25
e      0.75
dtype: float64
a    1.75
b    0.60
e    0.75
dtype: float64
a       1.40
b       0.48
d    6104.20
e       0.75
dtype: float64

You can see that the None item is excluded from the calculation.

If you want to include None skipna = False/True

print(df_ExistNone.mean(skipna = True))
print(df_ExistNone.mean(skipna = False))
a    1.75
b    0.60
e    0.75
dtype: float64
a   NaN
b   NaN
dtype: float64

Recommended Posts

Use Mean in DataFrame
Use DataFrame in Java
Use config.ini in Python
Use dates in Python
Use Valgrind in Python
Use ujson in requests
Use profiler in Python
Let's use def in python
Use Anaconda in pyenv environment
Use Measurement Protocol in Python
Use callback function in Python
Use parameter store in Python
Use HTTP cache in Python
Use regular expressions in C
Use MongoDB ODM in Python
Use list-keyed dict in Python
Use Random Forest in Python
Use regular expressions in Python
Use Spyder in Python IDE
Use Juman ++ in server mode
ยท Address already in use solution
Use <input type = "date"> in Flask
Pandas / DataFrame Tips for practical use
Use jinja2 template in excel file
Use optinal type-like in Go language
Use fabric as is in python (fabric3)
How to use classes in Theano
Mock in python-how to use mox
Use watchdog (watchmedo) in test-driven development
How to use SQLite in Python
Use rospy with virtualenv in Python3
Use API not implemented in twython
How to use Mysql in python
Use Python in pyenv with NeoVim
How to use ChemSpider in Python
How to use PubChem in Python
Use django-debug-toolbar in VirtualBox / Vagrant environment
Use OpenCV with Python 3 in Window
How to use calculated columns in CASTable
Calculation of mean IoU in object detection
Use the type features evolved in Sphinx-2.4
[Introduction to Python] How to use class in Python?
Use print in a Python2 lambda expression
Use of constraints file added in pip 7.1
[Python] View dataframe in VScode debug console
In gunicorn (> = 19.2), use max_requests_jitter along with max_requests
Use tensorflow in an environment without root
Easily use your own functions in Python
Use a scikit-learn model trained in PySpark
How to use Google Test in C
Resolve the Address already in use error
Easy way to use Wikipedia in Python
Minimum knowledge to use Form in Flask
How to reassign index in pandas dataframe
Use cartopy without bugs in Google Colaboratory
How to use Anaconda interpreter in PyCharm
R: Use Japanese instead of Japanese in scripts
Don't use \ d in Python 3 regular expressions!
How to use __slots__ in Python class
Use the command previously entered in IDLE
Use pathlib in Maya (Python 2.7) for upcoming Python 3.7