import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
%matplotlib inline
df = pd.read_csv("G:\learning python\medical-data visualizer/medical_examination.csv")
df["overweight"] = (df["weight"]/pow(df["height"]/100, 2) > 25).astype(int)
df["cholesterol"] = (df["cholesterol"] > 1).astype(int)
df["gluc"] = (df["gluc"] > 1).astype(int)
df_cat = pd.melt(df, id_vars =["cardio"], value_vars = ["cholesterol", "gluc", "smoke", "alco", "active", "overweight"])
df_cat = df_cat.groupby(['cardio','variable','value']).size()
print(df_cat)
这是我的系列:
cardio variable value
0 active 0 6378
1 28643
alco 0 33080
1 1941
cholesterol 0 29330
1 5691
gluc 0 30894
1 4127
overweight 0 15915
1 19106
smoke 0 31781
1 3240
1 active 0 7361
1 27618
alco 0 33156
1 1823
cholesterol 0 23055
1 11924
gluc 0 28585
1 6394
overweight 0 10539
1 24440
smoke 0 32050
1 2929
对于该系列中最后一个未命名的列,我想将其转换为列名称为cardio,变量,值和总数的数据框。我尝试使用.to_frame(),但数据框仅使用1个列名,因此无法正确放置所有四个列名。我怎样才能做到这一点?预先感谢!