Dataframe 有没有更好的方法在Python中查找摘要统计信息?
以下是我查找5点汇总统计信息的代码。我不断地发现这个错误:Dataframe 有没有更好的方法在Python中查找摘要统计信息?,dataframe,statistics,dataset,data-science,python-3.7,Dataframe,Statistics,Dataset,Data Science,Python 3.7,以下是我查找5点汇总统计信息的代码。我不断地发现这个错误: TypeError Traceback (most recent call last) <ipython-input-8-ba78d5218ead> in <module>() ----> 1 summary = [('items', describe('items')), 2 ('quantity', des
TypeError Traceback (most recent call last)
<ipython-input-8-ba78d5218ead> in <module>()
----> 1 summary = [('items', describe('items')),
2 ('quantity', describe('quantity')),
3 ('nic', describe('nic')),
4 ('act_cost', describe('act_cost'))]
<ipython-input-1-bcf37f98eb7d> in describe(key)
4 for i in scripts:
5 a.append(i[key])
----> 6 a=scripts[key]
7 total = sum(script[key] for script in scripts)
8 avg = total/len(a)
TypeError: list indices must be integers or slices, not str
列表索引必须是整数或片,而不是str
看起来我使用我创建的descripe函数的方式是错误的
from statistics import stdev,median,mean
def describe(key):
a=[]
for i in scripts:
a.append(i[key])
a=scripts[key]
total = sum(script[key] for script in scripts)
avg = total/len(a)
avg=mean(a)
s = stdev(a)
q25 = min(a)+(max(a)-min(a))*25
med = min(a)+(max(a)-min(a))*50
med=median(a)
q75 = min(a)+(max(a)-min(a))*75
return (total, avg, s, q25, med, q75)`enter code here`
summary = [('items', describe('items')),
('quantity', describe('quantity')),
('nic', describe('nic')),
('act_cost', describe('act_cost'))]
我不断地发现这个错误:
TypeError Traceback (most recent call last)
<ipython-input-8-ba78d5218ead> in <module>()
----> 1 summary = [('items', describe('items')),
2 ('quantity', describe('quantity')),
3 ('nic', describe('nic')),
4 ('act_cost', describe('act_cost'))]
<ipython-input-1-bcf37f98eb7d> in describe(key)
4 for i in scripts:
5 a.append(i[key])
----> 6 a=scripts[key]
7 total = sum(script[key] for script in scripts)
8 avg = total/len(a)
TypeError: list indices must be integers or slices, not str
TypeError回溯(最近一次调用)
在()
---->1摘要=[('items',description('items')),
2(“数量”,描述(“数量”),
3(“nic”,描述(“nic”),
4(“行为成本”,描述(“行为成本”)]
在描述中(关键)
4对于脚本中的i:
5 a.附加(i[键])
---->6 a=脚本[键]
7总计=总和(脚本中脚本的脚本[键])
8平均值=总计/长度(a)
TypeError:列表索引必须是整数或片,而不是str
很难理解您的问题,因为我们不知道脚本的样子。它是一个全局变量,未在脚本中定义。该错误表示脚本
属于列表
类型,但您似乎认为它是代码中的数据帧
。因此,请检查脚本的类型
另外,您是否知道有一种简单的方法可以使用numpy
计算五位数摘要,如下所示:
import numpy as np
minimum, q25, med, q75, maximum = np.percentile(a, [0, 25, 50, 75, 100], interpolation='midpoint')
有关说明,请参阅:
根据您的问题,您正在访问词典列表。
直接使用其键值进行访问不会在此处产生结果
所以你必须这么做,
getValues=lambda key,inputData:[subVal[key]对于inputData中的subVal,如果key in subVal]
在这种情况下,,
getValues('key',scripts)将给出相应的列表,然后很容易计算该列表的统计信息