列表中的条件lambda函数?大熊猫

时间:2020-03-23 05:47:16

标签: python pandas lambda

enter image description here

我想找到使用lambda函数的狗的平均年龄,

到目前为止,我已经编写了这段代码

#FIXED EFFECT OR WITHIN ESTIMATOR fixed <-plm(GDP_growth ~ gfdddi01 + gfdddi02 + gfdddi04 + gfdddi05, data = pdata, model ="within") summary(fixed) ## Oneway (individual) effect Within Model ## ## Call: ## plm(formula = GDP_growth ~ gfdddi01 + gfdddi02 + gfdddi04 + gfdddi05, ## data = pdata, model = "within") ## ## Balanced Panel: n = 17, T = 41, N = 697 ## ## Residuals: ## Min. 1st Qu. Median 3rd Qu. Max. ## -18.89148 -1.17470 0.12701 1.48874 20.70109 ## ## Coefficients: ## Estimate Std. Error t-value Pr(>|t|) ## gfdddi01 -0.0066663 0.0153800 -0.4334 0.6648 ## gfdddi02 0.0051626 0.0153343 0.3367 0.7365 ## gfdddi04 -0.0245573 0.0150069 -1.6364 0.1022 ## gfdddi05 -0.0049627 0.0073786 -0.6726 0.5014 ## ## Total Sum of Squares: 5421.5 ## Residual Sum of Squares: 5366.8 ## R-Squared: 0.010095 ## Adj. R-Squared: -0.019192 ## F-statistic: 1.72352 on 4 and 676 DF, p-value: 0.14296

...但是它一直给我一个值错误..... 帮助会非常有用!

2 个答案:

答案 0 :(得分:1)

如果需要带狗的age和可能的另一个值,则用boolean indexingDataFrame.loc进行过滤:

out = df.loc[df['pets'].apply(lambda x: 'dog' in x), 'age'].mean()
print (out)
46.75

如果需要age,只有一只狗:

out = df.loc[df['pets'].apply(lambda x: x[0] == 'dog'), 'age'].mean()

out = df.loc[df['pets'].str[0] == 'dog', 'age'].mean()
print (out)
55.333333333333336

答案 1 :(得分:0)

您也可以在没有任何lambda函数的情况下完成此工作。 可能的解决方案之一是:

df.explode('pets').query('pets == "dog"').age.mean()