简体   繁体   English

对具有相同名称的pandas数据框中的列执行功能

[英]Perform a function on columns in pandas dataframe with the same name

I have a dataframe which contains 111 columns in which some of them have the same column names. 我有一个包含111列的数据框,其中某些列具有相同的列名。 The total unique column names are 27. 唯一列总数为27。

>>> has_2.head(6)
    Has_MCS_A      Has_MCS_A     Has_MCS_A      Has_MCS_A  \
           0              0              0              3   
           0              1              0              0   
           0              0              0              0   
           1              0              0              0   
           0              0              10             0   
           0              0              0              0   

    Has_MCS_B     Has_MCS_B         Has_MCS_B        Has_MCS_B  \
          0                0                0                6   
          0                0                0                0   
          0                9                0                0   
          10               0                0                0   
          0                0                0                0   
          0                0                7                0   

I want to add the values in these columns with the same column name. 我想在这些列中使用相同的列名添加值。 So finally the result should be a dataframe with only 27 columns 所以最终结果应该是只有27列的数据框

You can construct a new df and iterate over the unique column values and then assign for each column the sum row-wise: 您可以构造一个新的df并遍历唯一的列值,然后为每列逐行分配sum

In [21]:
import io
import pandas as pd
t="""Has_MCS_A      Has_MCS_A     Has_MCS_A      Has_MCS_A 
        0              0              0              3   
           0              1              0              0   
           0              0              0              0   
           1              0              0              0   
           0              0              10             0   
           0              0              0              0   """
df = pd.read_csv(io.StringIO(t), sep='\s+')
df

Out[21]:
   Has_MCS_A  Has_MCS_A.1  Has_MCS_A.2  Has_MCS_A.3
0          0            0            0            3
1          0            1            0            0
2          0            0            0            0
3          1            0            0            0
4          0            0           10            0
5          0            0            0            0

In [22]:    
# overwrite the columns to force duplicate names
df.columns = ['Has_MCS_A','Has_MCS_A','Has_MCS_A','Has_MCS_A']
df

Out[22]:
   Has_MCS_A  Has_MCS_A  Has_MCS_A  Has_MCS_A
0          0          0          0          3
1          0          1          0          0
2          0          0          0          0
3          1          0          0          0
4          0          0         10          0
5          0          0          0          0
In [23]:
# construct a new df
new_df = pd.DataFrame()
for col in df.columns.unique():
    new_df[col] = df[col].sum(axis=1)
new_df

Out[23]:
   Has_MCS_A
0          3
1          1
2          0
3          1
4         10
5          0

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

相关问题 在 Pandas 数据框中取消旋转多个具有相同名称的列 - Unpivot multiple columns with same name in pandas dataframe 如何对Python Pandas中同一个dataframe中的两列进行运算? - How to perform an operation with two columns in the same dataframe in Python Pandas? 写一个function对一个Pandas中的多列进行计算 dataframe - Write a function to perform calculations on multiple columns in a Pandas dataframe 如何在 Pandas DataFrame 上动态添加同名列? - How to dynamically add columns with same name on a pandas DataFrame? 如何将同名的熊猫列替换到另一个数据框中? - How to replace pandas columns with the same name in to another dataframe? 将同名pandas数据框列的值聚合到单列 - Aggregate values of same name pandas dataframe columns to single column 熊猫合并名称相同但行不同的DataFrame列 - Pandas Merge DataFrame Columns With Same Name But Different Rows 如何使用具有相同名称/标识符的多个列创建Pandas DataFrame - How to create Pandas DataFrame with multiple columns that have the same name/indentifier 无法在 pandas dataframe 中重新排列具有相同名称的列 Python - Trouble rearranging columns with same name in pandas dataframe in Python 如何在Pandas DataFrame中执行列的条件添加? - How to perform conditional addition of columns in Pandas DataFrame?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM