简体   繁体   English

是否有一种更简洁的方法可以根据字符串中的特定子字符串获取多个变量的平均值

[英]Is there a more concise way of taking the mean of multiple variables based on a specific sub-string from a string

I have variables associated with a name that i want to take the mean of, based on its MainName.我有一个与名称相关联的变量,我想根据它的 MainName 取其平均值。 Noting that i have more than two MainNames as opposed to the example below, and would look messy doing all of it.请注意,与下面的示例相反,我有两个以上的 MainNames,并且在执行所有操作时看起来很混乱。 So i was wondering if anyone could make this more concise?所以我想知道是否有人能让这更简洁? Thanks in advance!提前致谢!

fullname = ['MainName1,subname1','MainName1,subname2','MainName2,subname1','MainName2,subname2']
var1 = [1,5,9,4]
var2 = [2,6,1,5]
var3 = [3,7,2,6]
var4 = [4,8,3,7]

    vars = pd.DataFrame(np.column_stack([fullname,var1,var2,var3,var4]))
    vars = vars.set_index('fullname')

    meanvars = [(allvars[allvars.index.str.contains('MainName1')]).mean(),
                (allvars[allvars.index.str.contains('MainName2')]).mean()]
    MainName = ['MainName1','MainName2']

    Final = pd.DataFrame(np.column_stack([MainName,meanvars]))

You can use str.extract for get substrings with joined substrings from list joined by |您可以使用str.extract从由|连接的列表中获取带有连接子字符串的子字符串| for regex OR passed to groupby with aggregating mean :对于正则表达式OR通过聚合mean传递给groupby

fullname = ['MainName1,subname1','MainName1,subname2',
            'MainName2,subname1','MainName2,subname2']
var1 = [1,5,9,4]
var2 = [2,6,1,5]
var3 = [3,7,2,6]
var4 = [4,8,3,7]

df = pd.DataFrame(np.column_stack([var1,var2,var3,var4]), index=fullname)
print (df)
                    0  1  2  3
MainName1,subname1  1  2  3  4
MainName1,subname2  5  6  7  8
MainName2,subname1  9  1  2  3
MainName2,subname2  4  5  6  7

L = ['MainName1','MainName2']
idx = df.index.str.extract('('+ '|'.join(L) + ')', expand=False)
print (idx)
Index(['MainName1', 'MainName1', 'MainName2', 'MainName2'], dtype='object')

df = df.groupby(idx).mean()
print (df)
             0    1    2    3
MainName1  3.0  4.0  5.0  6.0
MainName2  6.5  3.0  4.0  5.0

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在 python 中以简洁的方式更改特定的子字符串? - how to change specific sub-string in a concise way in python? 当我们有多个子字符串时,如何使用python替换字符串的特定子字符串? - How to replace specific sub-string of a string using python while we have multiple sub-string? 解析字符串中特定子字符串的最pythonic方法? - The most pythonic way to parse specific sub-string in a string? 从python中的字符串中获取特定的子字符串 - Take a specific sub-string from a string in python 根据具有子字符串的特定值删除字典项 - Removing dictionary items based on specific values having a sub-string 根据列子字符串删除记录 - Drop records based on column sub-string 从字符串的开头删除子字符串 - Remove sub-string from beginning of string 基于掩码删除日期子字符串 - Remove date sub-string based on a mask PANDAS:根据条件将子字符串的一部分移动到字符串的末尾 - PANDAS : move a part of a sub-string to the end of the string based on condition 如何通过 Python 中的 Selenium 从相对于多个分隔符动态变化的字符串中检索子字符串 - How to retrieve a sub-string from a string that changes dynamically with respect to multiple delimiters through Selenium in Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM