You can use the sum() function to calculate the sum of values in each row for columns that have 'max' in their name. Here's an example code:

import pandas as pd

# Create sample data
data = {
    'Type 1': ['Bug', 'Dark', 'Dragon'],
    'HP': [86, 126, 125],
    'Attack_max': [185, 150, 180],
    'Attack_min': [10, 50, 50],
    'Attack_mean': [70.971014, 88.387097, 112.125000],
    'Defense_max': [230, 125, 130],
    'Defense_min': [30, 30, 35],
    'Defense_mean': [70.724638, 70.225806, 86.375000]
}

df = pd.DataFrame(data)

# Calculate the sum of values in each row for columns with 'max' in their name
df['max_sum'] = df[['Attack_max', 'Defense_max']].sum(axis=1)

print(df)

Output:

   Type 1   HP  Attack_max  Attack_min  Attack_mean  Defense_max  Defense_min  Defense_mean  max_sum
0     Bug   86         185          10    70.971014          230           30     70.724638      415
1    Dark  126         150          50    88.387097          125           30     70.225806      275
2  Dragon  125         180          50   112.125000          130           35     86.375000      310

In this example, we first create a DataFrame with sample data. Then, we use the sum() function to calculate the sum of values in each row for columns that contain 'max' in their name, storing the result in a new column called 'max_sum'. Finally, we print the DataFrame to view the results.

Pandas: How to Sum Values in Each Row for Columns with 'max' in Their Name

原文地址: https://www.cveoy.top/t/topic/futC 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录