我试图编写一个函数来计算一个比值比,给定一个字符串,该字符串与一个与平均值相关的熊猫系列中的一个索引相匹配。我想找到与字符串匹配的值,对其执行计算。然后取大熊猫系列中与字符串不匹配的每个值的平均值并在那里执行计算。我有什么
def ratio(m, series):
Numerator = [series.Value[str.contains(motif)].mean()] for motif in series.index
Denominator = [series.Value[str.contains(motif, case = False).mean()] for motif in series.index
ratio = numerator/denominator
return ratio
不熟悉熊猫系列并且遇到很多麻烦。
答案 0 :(得分:0)
尝试:
def ratio(m, series):
Numerator = [series.Value.loc[series.index == motif].mean()] for motif in series.index
Denominator = [series.Value.loc[series.index != motif].mean()] for motif in series.index
ratio = numerator/denominator
return ratio