我正在尝试创建一个程序来查找数据文件的最大值,最小值,平均值和bmi。该文件是12列宽,但是,我不知道如何使用数据为每一行创建一个bmi? 旁注: 我不能使用Pandas,我需要用标准线切片等解决。
这里是示例data.txt:
Name Height(m) Weight(kg)
Joe 1.82 72.57
Mary 1.60 63.50
Dion 1.90 90.71
Kayla 1.72 66.31
Jose 1.78 70.23
Sofia 1.63 65.12
Erik 1.98 92.21
Sara 1.57 65.77
example result:
Name Height(m) Weight(kg) BMI
Joe 1.82 72.57 21.91
Mary 1.60 63.50 24.80
Dion 1.90 90.71 25.13
Kayla 1.72 66.31 22.41
Jose 1.78 70.23 22.17
Sofia 1.63 65.12 24.51
Erik 1.98 92.21 23.52
Sara 1.57 65.77 26.68
Average 1.75 73.30 23.89
Max 1.98 92.21 26.68
Min 1.57 63.50 21.91
到目前为止,这是我的代码:
def reading_file():
file=open("data.txt")
headers = file.readline().strip()
print("headers:\n{}\n\nrest of file:".format(headers))
total_height=0
total_weight=0
bmi=0
for line in file:
line_list=line.strip()
height=line_list[12:24]
weight=line_list[24:36]
print(height,weight)
total_height+=float(height)
avg_height=total_height/8
total_weight+=float(weight)
avg_weight=total_weight/8
avg_bmi=float(avg_weight)/float(avg_height)**2
print("average bmi:",avg_bmi)
print("average weight:",avg_weight)
print("average height:",avg_height)
print(reading_file())
我的结果: 标题: 名称高度(m)重量(kg)
rest of file:
1.82 72.57
1.60 63.50
1.90 90.71
1.72 66.31
1.78 70.23
1.63 65.12
1.98 92.21
1.57 65.77
average bmi: 23.93551020408163
average weight: 73.3025
average height: 1.75
None
如何为每列创建bmi?
答案 0 :(得分:0)
您可以使用pandas:
df = pd.read_clipboard()
df
Out[20]:
Name Height(m) Weight(kg)
0 Joe 1.82 72.57
1 Mary 1.60 63.50
2 Dion 1.90 90.71
3 Kayla 1.72 66.31
4 Jose 1.78 70.23
5 Sofia 1.63 65.12
6 Erik 1.98 92.21
7 Sara 1.57 65.77
# calculate the BMI column
df['BMI'] = df['Weight(kg)'] / (df['Height(m)'] ** 2)
df
Out[22]:
Name Height(m) Weight(kg) BMI
0 Joe 1.82 72.57 21.908586
1 Mary 1.60 63.50 24.804687
2 Dion 1.90 90.71 25.127424
3 Kayla 1.72 66.31 22.414143
4 Jose 1.78 70.23 22.165762
5 Sofia 1.63 65.12 24.509767
6 Erik 1.98 92.21 23.520559
7 Sara 1.57 65.77 26.682624
# use the describe() method to, well, *describe* the numerical columns in your file
df.describe()
Out[23]:
Height(m) Weight(kg) BMI
count 8.000000 8.000000 8.000000
mean 1.750000 73.302500 23.891694
std 0.147067 11.583038 1.680321
min 1.570000 63.500000 21.908586
25% 1.622500 65.607500 22.352048
50% 1.750000 68.270000 24.015163
75% 1.840000 77.105000 24.885372
max 1.980000 92.210000 26.682624
我将 意味着,最大值和分钟作为练习留给你:)