简体   繁体   English

使用python pandas按名称计算多个列

[英]Calculate multiple columns by names using python pandas

I have a dataframe similar like this, 我有一个类似这样的数据帧,

cat_A  cat_B  cat_C  cat_D  dog_A  dog_B  dog_C  dog_D
  3      2      4      1      9      8      10     6
 ...
 ...

I knew how to calculate between columns by using column names, like 我知道如何使用列名称在列之间进行计算,例如

df['ratio_A'] = df['cat_A']/df['dog_A']

cat_A  cat_B  cat_C  cat_D  dog_A  dog_B  dog_C  dog_D  ratio_A
  3      2      4      1      9      8      10     6      3/9

But when I tried to generate multiple columns by calculate each of those columns, are there any other easier ways to calculate all columns and append new columns by once? 但是,当我尝试通过计算每个列来生成多个列时,是否还有其他更简单的方法来计算所有列并一次追加新列? Instead of 代替

df['ratio_B'] = df['cat_B']/df['dog_B']

df['ratio_C'] = df['cat_C']/df['dog_C']

df['ratio_D'] = df['cat_D']/df['dog_D']

When the column length become very large it will be a lot of lengthy code to copy and paste. 当列长度变得非常大时,复制和粘贴将会有很多冗长的代码。 Do I need to create 2 lists like, 我是否需要创建2个列表,

l1 = [cat_A, cat_B, cat_C, cat_D], l2= [dog_A, dog_B, dog_C, dog_D]

Then using for loops to implement? 然后使用for循环来实现?

IMO a good practice here would be to work with MultiIndex es instead of flat columns: IMO这里的一个好习惯是使用MultiIndex es而不是flat column:

df.columns = pd.MultiIndex.from_tuples(map(tuple, df.columns.str.split('_')))
df
  cat          dog          
    A  B  C  D   A  B   C  D
0   3  2  4  1   9  8  10  6

At this point, computing the ratio is very simple courtesy index alignment. 在这一点上,计算比例是非常简单的礼貌索引对齐。

df['cat'] / df['dog']
          A     B    C         D
0  0.333333  0.25  0.4  0.166667

res =  df['cat'] / df['dog']
res.columns = pd.MultiIndex.from_product([['ratio'], res.columns])

pd.concat([df, res], axis=1)
  cat          dog               ratio                     
    A  B  C  D   A  B   C  D         A     B    C         D
0   3  2  4  1   9  8  10  6  0.333333  0.25  0.4  0.166667

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

相关问题 如何使用除第一个包含名称的列之外的所有列的 pandas 计算 python 中的累积总和? - How to calculate cumulative sum in python using pandas of all the columns except the first one that contain names? 使用 Pandas 将 2 列名称转换为 4 列名称 - Converting 2 columns of names into 4 columns of names using Pandas 如何在不使用列名的情况下对 Pandas 中的多个列执行操作? - How to perform operation to multiple columns in Pandas without using column names? 如何使用Pandas Python中的分组列计算百分比? - How to calculate a percentage using grouped columns in Pandas python? Python Pandas-使用两列中的条件计算平均值 - Python Pandas - Calculate mean using criteria from two columns 如何使用python pandas计算给定列的特定行的百分比? - how to calculate percentage for particular rows for given columns using python pandas? Python Pandas - 使用 .loc 在多列上使用 AND 和 OR 进行选择 - Python Pandas - using .loc to select with AND and OR on multiple columns Python Pandas 只需使用 lambda 读取名称列 - Python Pandas just read columns with names using lambda 如何使用python和pandas对多列进行分组 - how to groupby multiple columns using python and pandas 使用 python pandas dataframe 中的多个列进行汇总 - Summarize using multiple columns in python pandas dataframe
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM