Python Forum
How to calculate column mean and row skip non numeric and na
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to calculate column mean and row skip non numeric and na
#1
Hi,
I want to calculate column mean and row mean & skip "na" and "non-numeric". I use below code, but it gives some warning as show below:

__main__:5: FutureWarning: convert_objects is deprecated. Use the data-type specific converters pd.to_datetime, pd.to_timedelta and pd.to_numeric.

import pandas as pd
df = pd.read_csv(r"D:\Data\PythonCodes\outsummary2.csv")


print((df[['s2']].convert_objects(convert_numeric=True)).mean(skipna=True))
my csv data:

	s1	s2	s3
A	1	2	1
B	na	5	3
U	1	na	0
Z	0		
Z	0	2	2
is there any efficient way to skip "non-numeric" and "na", while calculating column mean (or sum)
Reply
#2
pd.to_numeric(df['s2'], errors='coerce').mean()
Reply
#3
It works, Thanks a lot. Can we get the row index of actual numeric values?
Reply
#4
>>> df
    s1
0   1
1  na
2   1
3   0
4   0
>>> for i, num in df['s1'].iteritems():
...   if type(num) is int:
...     print(i)
...
0
2
3
4
Reply
#5
import pandas as pd
df = pd.read_csv(r"D:\Data\outsummary2.csv")
df2 = df.convert_objects(convert_numeric=True)
for i, num in df2['s2'].iteritems():
    print(num, type(num))
    if type(num) is int:
        print(i)
Output:
it always float type.
2.0 <class 'numpy.float64'>
5.0 <class 'numpy.float64'>
nan <class 'numpy.float64'>
nan <class 'numpy.float64'>
2.0 <class 'numpy.float64'>
Reply
#6
>>> import numpy as np
>>> df2
0    2.0
1    5.0
2    NaN
3    2.0
Name: s2, dtype: float64
>>> for i, num in df2.iteritems():
...   if not np.isnan(num):
...     print(num)
...
2.0
5.0
2.0
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Numeric Enigma Machine idev 9 491 Mar-29-2024, 06:15 PM
Last Post: idev
Question Numeric Anagrams - Count Occurances monty024 2 1,505 Nov-13-2021, 05:05 PM
Last Post: monty024
  How to get datetime from numeric format field klllmmm 3 2,001 Nov-06-2021, 03:26 PM
Last Post: snippsat
  need to skip password prompt, continue... tester_V 2 1,463 Oct-19-2021, 05:49 PM
Last Post: tester_V
  Delimiters - How to skip some html tags from being translate Melcu54 0 1,651 May-26-2021, 06:21 AM
Last Post: Melcu54
  How to skip to a different line while importing a different script kat_gamer 2 2,237 Feb-03-2021, 04:10 AM
Last Post: deanhystad
  Calculate column with cumulative return tgottsc1 1 1,855 Jan-25-2021, 12:52 PM
Last Post: buran
  Extract continuous numeric characters from a string in Python Robotguy 2 2,631 Jan-16-2021, 12:44 AM
Last Post: snippsat
  How to skip a folder directory in a loop mfkzolo 2 12,535 Nov-18-2020, 07:56 AM
Last Post: mfkzolo
  How to skip LinkedIn signup link using python script? Mangesh121 0 1,792 Aug-26-2020, 01:22 PM
Last Post: Mangesh121

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020