######原文件在这
[](https://share.weiyun.com/3MLuaXO5
)
###### def Obesity_judge(BMI):
if BMI<18.5:
return("Underweight")
if 18.5<=BMI<=24.9:
return("Normal weight")
if 25<=BMI<=29.9:
return("Overweight")
if BMI>=30:
return("Obesity")
file = open("BMI data.txt", "r")
f = open("I am here.txt", "w")
a=file.readlines()
for i in a:
b = i.split()
print(b)
if b[0]=="No.":
f.write("{:6}\t{:6}\t{:6}\t{:6}\t{:6}\t{:6}\t{:6}\t{:8}\n".format('No.','sex','height','weight','SBP','DBP','BMI','Categories'))
else:
f.write("{:6}\t".format(b[0]))
weight = float(b[3])
height = float(b[2])
c = height / 100
BMI = weight / c ** 2
d = round(BMI, 1) #小数点1位
if b[1]=="1":
f.write("{:6}\t".format("Male"))
else:
f.write("{:6}\t".format("Female"))
f.write("{:6}\t".format(b[2]))
f.write("{:6}\t".format(b[3]))
BP = b[4].split("/")
f.write("{:6}\t".format(BP[0]))
f.write("{:6}\t".format(BP[1]))
f.write("{:5}\t".format(d))
f.write("{:8}\n".format(Obesity_judge(d)))
file.close()
f.close()
import pandas as pd
from matplotlib import pyplot as plt
def Obesity_judge(series):
if series['BMI'] < 18.5:
return "Underweight"
if 18.5 <= series['BMI'] <= 24.9:
return "Normal weight"
if 25 <= series['BMI'] <= 29.9:
return "Overweight"
if series['BMI'] >= 30:
return "Obesity"
df = pd.read_csv(dirname + '\\BMI data.txt', sep = '\t')
df['BMI'] = round(df.weight / ((df['height'] / 100) ** 2), 1)
df['Categories'] = df.apply(Obesity_judge, axis = 1)
dfMale = df[df['sex'] == '1']['BMI']
dfFemale = df[df['sex'] == '0']['BMI']
plt.rcParams['font.sans-serif'] = ['SimHei']
plt.rcParams['axes.unicode_minus'] = False
plt.title("男女生BMI值情况")
plt.xlabel('人数')
plt.ylabel('BMI值')
plt.plot(dfMale.values.tolist(), label='男生BMI')
plt.plot(dfFemale.values.tolist(), label='女生BMI')
plt.legend()
plt.show()
特定行特定列,可以使用pandas里面的loc