鱼C论坛

 找回密码
 立即注册
查看: 2244|回复: 1

[已解决]pandas列有空格时,求平均值出现问题,求大师帮忙,谢谢

[复制链接]
发表于 2023-4-8 22:53:42 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能^_^

您需要 登录 才可以下载或查看,没有账号?立即注册

x
data = {"A":['5','95','0.43','86.33','84.17','95','72.81','69.93','95','83.86','64.11','','89.16','','','0.11','','89.92','96','34.82','79.88','79.54','86.35','16.65','87.72'],
"B":['0','0','0','-8.67','-3.61','0','-6.49','0','0','0','0','','-1.35','','','0','','-8.34','0','0','0','-9.72','0','0','-7.28']}

df = pd.DataFrame(data)

求A列的平均数
求B列  大于0的个数,等于0的个数,小于0的个数,为空的个数

由于A列、B列都有空格,下面求值似乎用不了,求大师帮忙再看看,谢谢

# 计算 A 列的平均值
A_average = df['A'].astype(float).mean()
print(A_average)

# 计算 B 列的平均值
greater_than_zero = (df['B'].astype(float) > 0).sum()
equals_zero = (df['B'].astype(float) == 0).sum()
less_than_zero = (df['B'].astype(float) < 0).sum()
null_values = df['B'].isnull().sum()

print(greater_than_zero)
print(equals_zero)
print(less_than_zero)
print(null_values)
最佳答案
2023-4-8 22:55:58
您好!在这种情况下,您可以先将空字符串替换为numpy.nan,这样才能进行相应的计算。请尝试以下代码:
import pandas as pd
import numpy as np

data = {"A":['5','95','0.43','86.33','84.17','95','72.81','69.93','95','83.86','64.11','','89.16','','','0.11','','89.92','96','34.82','79.88','79.54','86.35','16.65','87.72'],
"B":['0','0','0','-8.67','-3.61','0','-6.49','0','0','0','0','','-1.35','','','0','','-8.34','0','0','0','-9.72','0','0','-7.28']}

df = pd.DataFrame(data)

# 将空字符串替换为 np.nan
df.replace("", np.nan, inplace=True)

# 计算 A 列的平均值
A_average = df['A'].astype(float).mean()
print(A_average)

# 计算 B 列的各项数值
greater_than_zero = (df['B'].astype(float) > 0).sum()
equals_zero = (df['B'].astype(float) == 0).sum()
less_than_zero = (df['B'].astype(float) < 0).sum()
null_values = df['B'].isnull().sum()

print(greater_than_zero)
print(equals_zero)
print(less_than_zero)
print(null_values)

这段代码应该能满足您的需求。如果您还有其他问题,请随时提问!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复

使用道具 举报

发表于 2023-4-8 22:55:58 | 显示全部楼层    本楼为最佳答案   
您好!在这种情况下,您可以先将空字符串替换为numpy.nan,这样才能进行相应的计算。请尝试以下代码:
import pandas as pd
import numpy as np

data = {"A":['5','95','0.43','86.33','84.17','95','72.81','69.93','95','83.86','64.11','','89.16','','','0.11','','89.92','96','34.82','79.88','79.54','86.35','16.65','87.72'],
"B":['0','0','0','-8.67','-3.61','0','-6.49','0','0','0','0','','-1.35','','','0','','-8.34','0','0','0','-9.72','0','0','-7.28']}

df = pd.DataFrame(data)

# 将空字符串替换为 np.nan
df.replace("", np.nan, inplace=True)

# 计算 A 列的平均值
A_average = df['A'].astype(float).mean()
print(A_average)

# 计算 B 列的各项数值
greater_than_zero = (df['B'].astype(float) > 0).sum()
equals_zero = (df['B'].astype(float) == 0).sum()
less_than_zero = (df['B'].astype(float) < 0).sum()
null_values = df['B'].isnull().sum()

print(greater_than_zero)
print(equals_zero)
print(less_than_zero)
print(null_values)

这段代码应该能满足您的需求。如果您还有其他问题,请随时提问!
想知道小甲鱼最近在做啥?请访问 -> ilovefishc.com
回复 支持 反对

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

小黑屋|手机版|Archiver|鱼C工作室 ( 粤ICP备18085999号-1 | 粤公网安备 44051102000585号)

GMT+8, 2024-11-14 20:49

Powered by Discuz! X3.4

© 2001-2023 Discuz! Team.

快速回复 返回顶部 返回列表