简体   繁体   English

填充 nan 值

[英]Filling nan values

I have a dataset that contains nan values.我有一个包含 nan 值的数据集。 These values are dependent on another variable, and I am trying to clean the data using it.这些值取决于另一个变量,我正在尝试使用它来清理数据。 I write a code to replace the nan values but it doesn't work.我编写了一个代码来替换 nan 值,但它不起作用。 The code is:代码是:

df.loc[(df["house"]=="rented") & (df["car"]=="yes")]["debt"].fillna(2, inplace=True)

https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.loc.html

Conditional that returns a boolean Series with column labels specified返回指定列标签的 boolean 系列的条件

df.loc[df['shield'] > 6, ['max_speed']]

            max_speed
sidewinder          7

Based on the documentation it should be converted to this:根据文档,它应该转换为:

df.loc['filter','selected column']

Give it a try like this:像这样试一试:

df.loc[(df["house"]=="rented") & (df["car"]=="yes"), ["debt"]].fillna(2, inplace=True)

Switch df.loc to将 df.loc 切换到

for val in df.index:
    if (df["house"][val] == "rented") and (df["car"][val] == "yes"):
        df["debt"][val] = 2

If I understand you correctly, you do not want to just fill in the na values.如果我理解正确,您不想只填写 na 值。 Rather, you'd like to fill the na values only when house is rented and you have a car.相反,您只想在房屋出租并且您有车时填写 na 值。 To fill all na values at df index "debt"填充 df 索引 "debt" 处的所有 na 值

df["debt"].fillna(2, inplace=True)

should be used rather then your second line of code.应该使用而不是你的第二行代码。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM