在pandas列中使用mean填充数字数据类型,使用mode填充非数字数据类型

2024-05-04 01:49:45 发布

您现在位置:Python中文网/ 问答频道 /正文

    # handling missing values
    class handling_missing_data():
    
      # Imputation
      # Handling columns which have null values
      
      for col in df[:]:
        if ():
    
          if (pd.df[:].astype(pd.Series([np.integer]))):
    
            df = df.select_dtypes(include=[np.integer]).fillna(df.select_dtypes(include=[np.integer]).mean().iloc[0], inplace=True)
      
          elif (pd.df[:].astype(pd.Series([object, str]))):
            
            df = df.select_dtypes(include=['object', 'str']).fillna(df.select_dtypes(include=['object', 'str']).mode().iloc[0], inplace=True)
      
      print(df.head())

为什么这段代码不起作用,因为我正在尝试识别特定列中dataset中缺少的值,并通过使用循环使用基于列数据类型的mean、mode填充缺少的列值。我正在寻找一些通用的方法


Tags: dfifobjectincludenpintegerselectseries
1条回答
网友
1楼 · 发布于 2024-05-04 01:49:45

您的代码有太多错误,无法在回答中合理地进行检查,所以让我来告诉您我将如何做

# Select numeric columns.
a = df.select_dtypes('number')
# Select string and object columns.
b = df.select_dtypes('object')

# Fill numeric columns with mean.
df[a.columns] = a.fillna(a.mean())
# Fill object columns with mode.
df[b.columns] = b.fillna(b.agg(lambda x: x.mode().values[0])

相关问题 更多 >