如何修复错误 DataError:没有要聚合的数字类型

发布于 2025-01-12 10:11:33 字数 183 浏览 0 评论 0原文

我需要按 addr_state 列分组。并对 Annual_inc 列应用聚合函数中位数。我的代码:

ax = Loan_df.groupby('addr_state')['annual_inc'].aggregate(['median']).plot.bar()

但我收到错误: DataError:没有要聚合的数字类型

I need to Group by addr_state column. and apply aggregate function median on annual_inc column. My code:

ax = loan_df.groupby('addr_state')['annual_inc'].aggregate(['median']).plot.bar()

But I am getting the error: DataError: No numeric types to aggregate

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情感失落者 2025-01-19 10:11:33
  • 您尚未提供样本数据。已模拟我可以暗示
  • 这取决于数据框中的数据。下面模拟了第二次尝试创建绘图时的一些错误数据
import pandas as pd
import numpy as np

loan_df = pd.DataFrame({"addr_state":np.repeat(list("ABCDE"),10), "annual_inc":np.random.randint(30,50,50)})
ax = loan_df.groupby('addr_state')['annual_inc'].aggregate(['median']).plot.bar()

# make random values in data none numeric
loan_df.loc[loan_df.sample(3).index, "annual_inc"] = "#n/a"
try:
    ax = loan_df.groupby('addr_state')['annual_inc'].aggregate(['median']).plot.bar()
except TypeError:
    print("bad data...")

  • you have not provided sample data. Have simulated what I can imply
  • this is down to data in your data frame. Simulated below some bad data on second attempt to create plot
import pandas as pd
import numpy as np

loan_df = pd.DataFrame({"addr_state":np.repeat(list("ABCDE"),10), "annual_inc":np.random.randint(30,50,50)})
ax = loan_df.groupby('addr_state')['annual_inc'].aggregate(['median']).plot.bar()

# make random values in data none numeric
loan_df.loc[loan_df.sample(3).index, "annual_inc"] = "#n/a"
try:
    ax = loan_df.groupby('addr_state')['annual_inc'].aggregate(['median']).plot.bar()
except TypeError:
    print("bad data...")

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文