不知道为什么我收到TypeError:sum()最多接受2个参数(给定3个)。我的代码正确吗?
调查不同年龄组的体重分布。首先,通过过滤df将数据集划分为三个DataFrame,并将它们分配给以下变量:df_20s:仅'age'小于30的行df_30s:仅“age”大于或等于30且小于40 df_40s的行:仅'age'大于或等于40(包括年龄50)的行要验证筛选是否正确,请打印以下结果:三个DataFrame的长度之和(总共应该有10,000行)df_20s的“age”列中的最小值和最大值df_30s的“age”列中的最小值和最大值df_40s的“age”列中的最小值和最大值预编码已经包含一个模板,供您打印结果;只要完成密码。
import pandas as pd
from matplotlib import pyplot as plt
df = pd.read_csv('/datasets/height_weight.csv')
df_20s = (df[df['age'] < 30])
df_30s = (df[(df['age'] >= 30) & (df['age'] < 40)])
df_40s = (df[df['age'] >= 40])
# print out the results
print("Sum of data frame lengths:",sum(len(df_20s),len(df_30s),len(df_40s)))
print("Min and max age for df_20s:", df_20s['age'].min(), df_20s['age'].max() )
print("Min and max age for df_30s:", df_30s['age'].min(), df_30s['age'].max() )
print("Min and max age for df_40s:", df_40s['age'].min(), df_40s['age'].max() )
追溯:
TypeError Traceback (most recent call last)
Input In [1], in <cell line: 13>()
9 df_40s [38;5;241m=[39m (df[df[[38;5;124m'[39m[38;5;124mage[39m[38;5;124m'[39m] [38;5;241m>[39m[38;5;241m=[39m [38;5;241m40[39m])
12 [38;5;66;03m# print out the results[39;00m
---> 13 [38;5;28mprint[39m([38;5;124m"[39m[38;5;124mSum of data frame lengths:[39m[38;5;124m"[39m,[38;5;28;43msum[39;49m[43m([49m[38;5;28;43mlen[39;49m[43m([49m[43mdf_20s[49m[43m)[49m[43m,[49m[38;5;28;43mlen[39;49m[43m([49m[43mdf_30s[49m[43m)[49m[43m,[49m[38;5;28;43mlen[39;49m[43m([49m[43mdf_40s[49m[43m)[49m[43m)[49m)
14 [38;5;28mprint[39m([38;5;124m"[39m[38;5;124mMin and max age for df_20s:[39m[38;5;124m"[39m, df_20s[[38;5;124m'[39m[38;5;124mage[39m[38;5;124m'[39m][38;5;241m.[39mmin(), df_20s[[38;5;124m'[39m[38;5;124mage[39m[38;5;124m'[39m][38;5;241m.[39mmax() )
15 [38;5;28mprint[39m([38;5;124m"[39m[38;5;124mMin and max age for df_30s:[39m[38;5;124m"[39m, df_30s[[38;5;124m'[39m[38;5;124mage[39m[38;5;124m'[39m][38;5;241m.[39mmin(), df_30s[[38;5;124m'[39m[38;5;124mage[39m[38;5;124m'[39m][38;5;241m.[39mmax() )
TypeError: sum() takes at most 2 arguments (3 given)
2条答案
按热度按时间m3eecexj1#
使用以下命令代替
sum(len(df_20s),len(df_30s),len(df_40s)))
:你也可以改进你的代码来使用字典和循环:
siv3szwd2#
这里的另一个解决方案是使用包含值的元组而不是列表:
通过将值括在括号中并将其作为元组传递给sum()函数,该函数将正确计算值的总和。