缺失行处理(R和python)

R(complete.cases)

复制代码
rm(list=ls())
# 创建一个包含缺失值的数据框
# df <- data.frame(
#   x = c(1, 2, NA, 4),
#   y = c(NA, 2, 3, 4),
#   z = c(1, NA, 3, 3)
# )
# 
# # 使用complete.cases函数筛选包含缺失值的数据行
# missing_rows <- !complete.cases(df)
# 
# # 打印包含缺失值的数据行
# print(df[missing_rows, ])

# create a data frame from scratch
age <- c(25, 999, 56,NA)
gender <- c("male", "female", "male", "male")
weight <- c(160, NA,110, 220)
mydata <- data.frame(age,gender,weight)

#print(mydata)
aa =mydata[complete.cases(mydata),] ## 直接取不缺少的行就可以了
print(aa)

结果如下

python(pd.dropna)

复制代码
import pandas as pd 
import numpy as np
df = pd.DataFrame({"age":[25,999,56,pd.NA],"gender":["male","female","male","male"],
                   "weight":[160,pd.NA,110,220]})
print(df)

# df = pd.DataFrame({"a": [0, pd.NA, 2], "b": [0, np.nan, 2]})
# df

df.dropna(axis = 0, how = 'any', inplace = True)
print(df)


相关推荐
工业互联网专业几秒前
基于django+vue的健身房管理系统-vue
vue.js·python·django·毕业设计·源码·课程设计·健身房管理系统
aischang18 分钟前
统信桌面专业版如何使用python开发平台jupyter
开发语言·python·jupyter·统信uos
红鼻子时代21 分钟前
Django RBAC项目后端实战 - 03 DRF权限控制实现
后端·python·django·rabc
敲键盘的小夜猫28 分钟前
大模型链路调试平台之LangSmith实战指南
python·langchain
狐凄40 分钟前
Python实例题:Python计算概率论
开发语言·python·概率论
Y31742942 分钟前
python Day46 学习(日志Day15复习)
python·学习·机器学习
这里有鱼汤43 分钟前
一文读懂量化交易中最常用的5种均线,附源码,建议收藏
后端·python
学不会就看1 小时前
selenium学习实战【Python爬虫】
python·学习·selenium
q567315231 小时前
分布式增量爬虫实现方案
开发语言·分布式·爬虫·python
勤奋的知更鸟1 小时前
LLaMA-Factory和python版本的兼容性问题解决
开发语言·python·llama-factory