以下是一个示例代码,演示如何按列项进行算术运算并进行汇总:
import pandas as pd
# 创建示例数据
data = {'A': [1, 2, 3, 4, 5],
'B': [6, 7, 8, 9, 10],
'C': [11, 12, 13, 14, 15]}
df = pd.DataFrame(data)
# 按列项进行算术运算并进行汇总
sum_of_columns = df.sum(axis=0)
mean_of_columns = df.mean(axis=0)
min_of_columns = df.min(axis=0)
max_of_columns = df.max(axis=0)
# 输出结果
print("Sum of columns:")
print(sum_of_columns)
print("Mean of columns:")
print(mean_of_columns)
print("Min of columns:")
print(min_of_columns)
print("Max of columns:")
print(max_of_columns)
这个示例代码使用pandas库创建了一个包含3列的数据框,然后使用sum()
、mean()
、min()
和max()
函数按列项对数据进行算术运算并进行汇总。最后,通过打印结果来展示每个汇总统计量。
输出结果类似于:
Sum of columns:
A 15
B 40
C 65
dtype: int64
Mean of columns:
A 3.0
B 8.0
C 13.0
dtype: float64
Min of columns:
A 1
B 6
C 11
dtype: int64
Max of columns:
A 5
B 10
C 15
dtype: int64
这些结果分别是每列的总和、平均值、最小值和最大值。
上一篇:按列文本进行行计数,并按组重置计数的结果收集到新列中
下一篇:按列写入CSV