2016-06-07 64 views
0

我有我不能制定得非常好比较愚蠢的问题时插入变量(我认为这解释了为什么我没有找到任何答案)的Python - 定义名称

我想计算平均值,最小值,并在我的数据帧熊猫系列的许多变量最大(比方说年龄和体重)

dataframe.age.min() 
dataframe.age.max() 
dataframe.age.mean() 

dataframe.weight.min() 
dataframe.weight.max() 
dataframe.weight.mean() 

我想创造一些类型的循环,这会做这样的事情:

list = ['age','weight'] 
for x in list: 
    min-"x" = dataframe.x.min() 
    max-"x" = dataframe.x.max() 
    mean-"x" = dataframe.x.mean() 

我想有变量称为min-age,max-age,平均年龄

我不明白如何定义一个函数,以及如何在名称min-“x”中插入我的名字变量(x)...

+1

你对这些变量做什么?它可能更好地填充字符串与var字符串名称的值作为您的计算结果 – EdChum

+0

我想打印它们在一个非常大的输出表... – salim

+1

[你不想这样做。](http ://stupidpythonideas.blogspot.cl/2013/05/why-you-dont-want-to-dynamically-create.html) –

回答

0

在您的数据框上使用describe然后处理索引。

dfd = df.describe().stack() 

dfd.index = dfd.index.to_series().str.join('-') 

count-age  10.000000 
count-weight 10.000000 
mean-age  -0.200662 
mean-weight  0.298352 
std-age   1.175323 
std-weight  0.901915 
min-age   -1.778043 
min-weight  -0.860798 
25%-age   -1.144173 
25%-weight  -0.488076 
50%-age   -0.092748 
50%-weight  0.294160 
75%-age   0.276348 
75%-weight  0.892405 
max-age   1.670823 
max-weight  1.680473 
dtype: float64