我在一个文本文件中有一组数据,排列在两列中。我想要的是计算第一列中重复数字的平均值。例如,前三行取第二列的平均值,依此类推。如果您能提供任何帮助,我将不胜感激。
0.628319 0.123401
0.628319 0.23044
0.628319 4.57734
0.888577 0.390783
1.40496 0.110672
1.40496 0.239377
1.40496 0.248376
1.40496 0.751108
1.40496 0.971678
1.40496 1.36865
将数据放入Excel文件中,并将其读取到Pandas DataFrame中。计算按第一列分组的第二列的平均值。
import pandas as pd
# header=None because there are no column headers in my XLSX file
# Column names will be integers: 0 and 1
data = pd.read_excel("physics.xlsx", header=None, engine="openpyxl")
# What does "grouped means" mean?: Sort column 1 values by column 0 value and take mean of each column-1 group
grp_means = data.groupby(0).mean()
print(grp_means)