如何在pandas中将一个值替换为NaN?
如何在pandas中将一个值替换为NaN?
我是pandas的新手,我正在尝试将csv文件加载到Dataframe中。我的数据中有缺失值,用?表示,我想将其替换为标准的缺失值NaN。请帮助我。我尝试阅读了Pandas文档,但是不太懂。
def readData(filename): DataLabels = ["age", "workclass", "fnlwgt", "education", "education-num", "marital-status", "occupation", "relationship", "race", "sex", "capital-gain", "capital-loss", "hours-per-week", "native-country", "class"] # ==== 试图使用na_values将?替换为NaN rawfile = pd.read_csv(filename, header=None, names=DataLabels, na_values=["?"]) age = rawfile["age"] print(age) print(rawfile[25:40]) #========试图替换? rawfile.replace("?", "NaN") print(rawfile[25:40]) return rawfile
数据:
[adult.data](http://archive.ics.uci.edu/ml/machine-learning-databases/adult/)
age workclass fnlwgt education education-num marital-status occupation relationship race sex capital-gain capital-loss hours-per-week native-country class 25 56 Local-gov 216851 Bachelors 13 Married-civ-spouse Tech-support Husband White Male 0 0 40 United-States >50K 26 19 Private 168294 HS-grad 9 Never-married Craft-repair Own-child White Male 0 0 40 United-States <=50K 27 54 ? 180211 Some-college 10 Married-civ-spouse ? Husband Asian-Pac-Islander Male 0 0 60 South >50K 28 39 Private 367260 HS-grad 9 Divorced Exec-managerial Not-in-family White Male 0 0 80 United-States <=50K 29 49 Private 193366 HS-grad 9 Married-civ-spouse Craft-repair Husband White Male 0 0 40 United-States <=50K