改变熊猫的nan值

时间:2018-11-23 08:59:19

标签: python pandas

在我的代码中,当df.dropna()方法起作用时,df.fillna()方法不起作用。我不想删除该列。 fillna()方法可以做什么?

def preprocess_df(df):
    for col in df.columns:  # go through all of the columns
        if col != "target":  # normalize all ... except for the target itself!
            df[col] = df[col].pct_change()  # pct change "normalizes" the different currencies (each crypto coin has vastly diff values, we're really more interested in the other coin's movements)
            # df.dropna(inplace=True)  # remove the nas created by pct_change
            df.fillna(method="ffill", inplace=True)
            print(df)
            break
            df[col] = preprocessing.scale(df[col].values)  # scale between 0 and 1.

2 个答案:

答案 0 :(得分:1)

它应该工作,除非它不在如上所述的循环内。

您应该在构造循环之前或在DataFrame构造期间考虑填充它:

下面的示例清楚地表明它可以正常工作:

>>> df
  col1
0  one
1  NaN
2  two
3  NaN

按预期工作:

>>> df['col1'].fillna( method ='ffill')  # This is showing column specific to `col1`

0    one
1    one
2    two
3    two
Name: col1, dtype: object

第二,如果您希望更改一些选择性的列,请使用以下方法:

假设您有3列,并且只想将fillnaffill用到2列。

>>> df
  col1  col2 col3
0  one  test  new
1  NaN   NaN  NaN
2  two  rest  NaN
3  NaN   NaN  NaN

定义要更改的列。

cols = ['col1', 'col2']

>>> df[cols] = df[cols].fillna(method ='ffill')
>>> df
  col1  col2 col3
0  one  test  new
1  one  test  NaN
2  two  rest  NaN
3  two  rest  NaN

如果您认为它会发生在整个DataFrame中,请按照以下步骤使用它:

>>> df
  col1  col2
0  one  test
1  NaN   NaN
2  two  rest
3  NaN   NaN

>>> df.fillna(method ='ffill')  # inplace=True if you considering as you wish for permanent change.
  col1  col2
0  one  test
1  one  test
2  two  rest
3  two  rest

答案 1 :(得分:0)

第一个值是NaN,所以我不得不使用 bfill 方法。谢谢大家