from sklearn.linear_model import LinearRegression
from sklearn.model_selection import train_test_split
freq_port = df.Sex.dropna().mode()[0] # mode返回出现最多的数据,col_name为列名
df['Sex'] = df['Sex'].fillna(freq_port) #采用出现最频繁的值插补
输出是---------------------------------------------------------------------------NameError Traceback (most recent call last)/tmp/ipykernel_98/2308284494.py in
1 from sklearn.linear_model import LinearRegression
2 from sklearn.model_selection import train_test_split
----> 3 freq_port = df.Sex.dropna().mode()[0] # mode返回出现最多的数据,col_name为列名
4 df['Sex'] = df['Sex'].fillna(freq_port) #采用出现最频繁的值插补
NameError: name 'df' is not defined
你要把所有代码放在同一行的格子里,不能分行。就不会报错了。
类似这样。
可以的话,把代码从头走一遍,可能是jupyter中途断开过一次,导致报错找不到df
一般df是指Pandas的DataFrame对象,你这代码里没看到引入Pandas,也没看到实例DataFrame,当然就没有数据了
df 是自定义的变量 没有定义这个变量