“`python pd.DataFrame 如何计算 mean(),同时忽略某些单元格中的 ‘NA’ 字符串 “`

huangapple go评论59阅读模式
英文:

pd.DataFrame how to calculate mean() while ignore 'NA' string in some cell

问题

我有一个数据框,我想计算列A和B的平均值,A和B中的某些行是字符串'NA',而其他行是numpy.float64类型。如何计算列A和B的均值,同时忽略那些'NA'?

我尝试将numeric_only设置为True,然后只返回列C和id。我期望A和B的均值分别为6.6和2.6。

fruit = pd.DataFrame({'id':(1,2,3,4,5,6),'Name':('apple','apple','melon','melon','orange','orange'), 'A': (1,2,'NA',20,5,5), 'B': (1,5,4,2,'NA',1) , 'C': (1,5,4,2,3,1)})

希望这对你有所帮助。

英文:

I have a dataframe that I want to calculate column mean of A and B,
some rows in A and B are of string 'NA', and others are of numpy.float64
how to calculate the column A and B while ignoring those 'NA'?
I tried to set numeric_only=True then it only return me column C and id.
i'm expecting A and B mean to be 6.6 and 2.6

fruit = pd.DataFrame({'id':(1,2,3,4,5,6),'Name':('apple','apple','melon','melon','orange','orange'), 'A': (1,2,'NA',20,5,5), 'B': (1,5,4,2,'NA',1) , 'C': (1,5,4,2,3,1)})

id Name A B C
1 apple 1 1 1
2 apple 2 5 5
3 melon 'NA' 4 4
4 melon 20 2 2
5 orange 5 'NA' 3
6 orange 5 1 1

答案1

得分: 1

尝试这个:

import numpy as np

fruit.replace('NA', np.nan, inplace=True)
fruit['A'].mean()
英文:

Try this:

import numpy as np

fruit.replace('NA', np.nan, inplace=True)
fruit['A'].mean()

答案2

得分: 1

由于 `mean` 可以跳过 NaN 值,您可以使用 `to_numeric` 并设置 `errors="coerce"`:

> errors{'ignore', 'raise', 'coerce'}, 默认为 'raise'
>
> - 如果为 'raise',则无效解析将引发异常。
> - **如果为 `coerce`,则无效解析将被设置为 NaN。**
> - 如果为 'ignore',则无效解析将返回输入。

    fruit[["A", "B"]].apply(pd.to_numeric, errors="coerce").mean()

输出:

    A    6.6
    B    2.6
    dtype: float64
英文:

Since mean can skip NaN values, you can use to_numeric and set errors="coerce" :

> errors{‘ignore’, ‘raise’, ‘coerce’}, default ‘raise’
>
> - If ‘raise’, then invalid parsing will raise an exception.
> - If coerce, then invalid parsing will be set as NaN.
> - If ‘ignore’, then invalid parsing will return the input.

fruit[["A", "B"]].apply(pd.to_numeric, errors="coerce").mean()

Output :

A    6.6
B    2.6
dtype: float64

答案3

得分: 0

fruit['A'].replace('NA', 0).sum() / sum(fruit['A'].ne('NA'))
# 6.6

fruit['B'].replace('NA', 0).sum() / sum(fruit['B'].ne('NA'))
# 2.6

所以:

fruit[['A', 'B']].apply(lambda c: c.replace('NA', 0).sum() / sum(c.ne('NA')))
A    6.6
B    2.6
dtype: float64
英文:
fruit['A'].replace('NA', 0).sum() / sum(fruit['A'].ne('NA'))
# 6.6

fruit['B'].replace('NA', 0).sum() / sum(fruit['B'].ne('NA'))
# 2.6

So :

fruit[['A', 'B']].apply(lambda c:c.replace('NA', 0).sum() / sum(c.ne('NA')))
A    6.6
B    2.6
dtype: float64

huangapple
  • 本文由 发表于 2023年2月18日 12:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/75491089.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定