处理熊猫中的空值–在一列中使用过滤后的值在其他两列中填充nan 问:包含一些缺失值的单独列 A:使用fillna和str.contains

如何解决处理熊猫中的空值–在一列中使用过滤后的值在其他两列中填充nan 问:包含一些缺失值的单独列 A:使用fillna和str.contains

这是我发布的a recent question/answer的澄清/重述。我想知道我的解决方案是最简单还是最有效的选择。

问:包含一些缺失值的单独列

我有一个包含三列的数据框:df.location,其字符串格式为逗号分隔的经度-纬度坐标,df.target,目标变量,其整数在1到5之间,当前格式为浮点数,以及df.null,该列主要是nan,但也混合了纬度-经度坐标,并且在1到5之间浮动。

这是一个df示例:

df = pd.DataFrame(
      {'target': {0: nan,1: nan,2: nan,3: nan,4: nan,5: 4.0,6: 5.0,7: 4.0,8: 4.0,9: 4.0},'location': {0: nan,5: '41.69230795,-72.26691314',6: '41.70631764,-70.2868794',7: '41.70687995,-70.28684036',8: '41.70598417,-70.28671793',9: '41.69220757,-70.26687248'},'null': {0: '41.70477575,-70.28844073',1: '2',2: '41.70637091,-70.28704334',3: '4',4: '3',5: nan,6: nan,7: nan,8: nan,9: nan}
      }
)

对于df.null中存在不丢失值的每一行,df.targetdf.location中的值均丢失。 (我不知道这是怎么发生的,但我检查了读取到Pandas Dataframe中的原始JSON,并确定当缺少位置和目标时,此null键会经常弹出。)这是Jupyter提供的Seaborn热图的屏幕截图笔记本来说明:

screenshot

可以安全地假设df.locationdf.target中的某些或所有缺失值都在df.null中吗?如果是这样,如何根据这些值是纬度字符串还是目标浮点数将这些值移到适当的列中?

A:使用fillna()和str.contains()

处理

到目前为止,这是我最好的答案-让我知道您的想法。基本上,我只是使用fillna(value=df.null)来填充df.locationdf.target中的所有缺失值:

df.target.fillna(
    value=df.null,inplace=True
)

df.location.fillna(
    value=df.null,inplace=True
)

然后,我使用正则表达式通过df.targetdf.location布尔过滤器,并将所有不适当的值设置为np.nan

# Converting columns to type str so string methods work
df = df.astype(str)

# Using regex to change values that don't belong in column to NaN
regex = '[,]'
df.loc[df.target.str.contains(regex),'target'] = np.nan
    
regex = '^\d\.?0?$'
df.loc[df.location.str.contains(regex),'location'] = np.nan
    
# Returning `df.level` to float datatype (str is the correct
# datatype for `df.location`
df.target.astype(float)

有更好的方法吗?

编辑:更改fillna()单元代码,使其起作用。

解决方法

可以安全地假设df.location和df.target中的某些或所有缺失值都在df.null中吗?

这取决于初始数据。如果您有太多需要手动检查的内容,您将不知道。您可以在转换后检查数据框,但不确定。

我有了fillna(value=)的新用法(感谢,我不太了解),我发现了一种更快的编写方法:

df = pd.DataFrame(
      {'target': {0: nan,1: nan,2: nan,3: nan,4: nan,5: 4.0,6: 5.0,7: 4.0,8: 4.0,9: 4.0},'location': {0: nan,5: '41.69230795,-72.26691314',6: '41.70631764,-70.2868794',7: '41.70687995,-70.28684036',8: '41.70598417,-70.28671793',9: '41.69220757,-70.26687248'},'null': {0: '41.70477575,-70.28844073',1: '2',2: '41.70637091,-70.28704334',3: '4',4: '3',5: nan,6: nan,7: nan,8: nan,9: nan}
      }
).assign(
    target=lambda x: x.target.fillna(value=pd.to_numeric(x.null,errors='coerce')),location=lambda x: x.location.fillna(
        value=x.loc[pd.to_numeric(x.null,errors='coerce').isnull(),'null']
    )
).drop('null',axis='columns')

前面的代码给出了以下数据框:

                   location  target
0  41.70477575,-70.28844073     NaN
1                       NaN     2.0
2  41.70637091,-70.28704334     NaN
3                       NaN     4.0
4                       NaN     3.0
5  41.69230795,-72.26691314     4.0
6   41.70631764,-70.2868794     5.0
7  41.70687995,-70.28684036     4.0
8  41.70598417,-70.28671793     4.0
9  41.69220757,-70.26687248     4.0

您可以通过检查以下内容来检查null和target中是否没有值:

  • 大于5的值(如果存在1,则您的假设为假,如果不是,则仍不确定:-))
  • 位置栏中的昏迷数。

我留下的旧版本给出相同的结果。

以前的版本

此处进行的转换中不含正则表达式:

import pandas as pd
from numpy import nan

df = pd.DataFrame(
      {'target': {0: nan,9: nan}
      }
).assign(
    # use the conversion to numeric of the null column in order to find values
    # going to target and to location
    new_target=lambda x: pd.to_numeric(x['null'],errors='coerce'),new_location=lambda x: x.loc[pd.to_numeric(x['null'],'null'],).assign(
    target_without_nan=lambda x: x.new_target.fillna(0.0),new_location=lambda x: x.new_location.fillna(''),target=lambda x: (x.target_without_nan + x.target.fillna(0.0)).loc[~(x.target.isnull() & x.new_target.isnull())],location=lambda x: x.location.fillna('').str.cat(x.new_location.astype(str)).replace('',nan)
).loc[:,['location','target']]

我使用this answer中的技巧进行求和和连接,以替换初始列的nan值。我还保留了nan的值,这些值在上一次分配目标时无法用.loc替换。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


依赖报错 idea导入项目后依赖报错,解决方案:https://blog.csdn.net/weixin_42420249/article/details/81191861 依赖版本报错:更换其他版本 无法下载依赖可参考:https://blog.csdn.net/weixin_42628809/a
错误1:代码生成器依赖和mybatis依赖冲突 启动项目时报错如下 2021-12-03 13:33:33.927 ERROR 7228 [ main] o.s.b.d.LoggingFailureAnalysisReporter : *************************** APPL
错误1:gradle项目控制台输出为乱码 # 解决方案:https://blog.csdn.net/weixin_43501566/article/details/112482302 # 在gradle-wrapper.properties 添加以下内容 org.gradle.jvmargs=-Df
错误还原:在查询的过程中,传入的workType为0时,该条件不起作用 <select id="xxx"> SELECT di.id, di.name, di.work_type, di.updated... <where> <if test=&qu
报错如下,gcc版本太低 ^ server.c:5346:31: 错误:‘struct redisServer’没有名为‘server_cpulist’的成员 redisSetCpuAffinity(server.server_cpulist); ^ server.c: 在函数‘hasActiveC
解决方案1 1、改项目中.idea/workspace.xml配置文件,增加dynamic.classpath参数 2、搜索PropertiesComponent,添加如下 <property name="dynamic.classpath" value="tru
删除根组件app.vue中的默认代码后报错:Module Error (from ./node_modules/eslint-loader/index.js): 解决方案:关闭ESlint代码检测,在项目根目录创建vue.config.js,在文件中添加 module.exports = { lin
查看spark默认的python版本 [root@master day27]# pyspark /home/software/spark-2.3.4-bin-hadoop2.7/conf/spark-env.sh: line 2: /usr/local/hadoop/bin/hadoop: No s
使用本地python环境可以成功执行 import pandas as pd import matplotlib.pyplot as plt # 设置字体 plt.rcParams['font.sans-serif'] = ['SimHei'] # 能正确显示负号 p
错误1:Request method ‘DELETE‘ not supported 错误还原:controller层有一个接口,访问该接口时报错:Request method ‘DELETE‘ not supported 错误原因:没有接收到前端传入的参数,修改为如下 参考 错误2:cannot r
错误1:启动docker镜像时报错:Error response from daemon: driver failed programming external connectivity on endpoint quirky_allen 解决方法:重启docker -> systemctl r
错误1:private field ‘xxx‘ is never assigned 按Altʾnter快捷键,选择第2项 参考:https://blog.csdn.net/shi_hong_fei_hei/article/details/88814070 错误2:启动时报错,不能找到主启动类 #
报错如下,通过源不能下载,最后警告pip需升级版本 Requirement already satisfied: pip in c:\users\ychen\appdata\local\programs\python\python310\lib\site-packages (22.0.4) Coll
错误1:maven打包报错 错误还原:使用maven打包项目时报错如下 [ERROR] Failed to execute goal org.apache.maven.plugins:maven-resources-plugin:3.2.0:resources (default-resources)
错误1:服务调用时报错 服务消费者模块assess通过openFeign调用服务提供者模块hires 如下为服务提供者模块hires的控制层接口 @RestController @RequestMapping("/hires") public class FeignControl
错误1:运行项目后报如下错误 解决方案 报错2:Failed to execute goal org.apache.maven.plugins:maven-compiler-plugin:3.8.1:compile (default-compile) on project sb 解决方案:在pom.
参考 错误原因 过滤器或拦截器在生效时,redisTemplate还没有注入 解决方案:在注入容器时就生效 @Component //项目运行时就注入Spring容器 public class RedisBean { @Resource private RedisTemplate<String
使用vite构建项目报错 C:\Users\ychen\work>npm init @vitejs/app @vitejs/create-app is deprecated, use npm init vite instead C:\Users\ychen\AppData\Local\npm-