如何根据数据类型交换列?

问题描述 投票:0回答:1

样本数据如下(数据是假的,不是真实数据):

钥匙 死亡指示器 死亡日期 确切的死亡日期 死亡原因
00 活着
02 死亡之家 2011年9月25日 N00
03 活着
09 死亡之家 J189 2015年8月28日
07 非居士死亡 2018年6月12日 C20

从表中可以看到,同一列内的数据类型并不一致。

Date Death
应采用日期格式;
Exact date of death
只能包含
Y
N
或空白;
Death Cause
应为字符串(即 ICD 代码)。

我尝试执行一些基本的数据清理:

Python:

import pandas as pd

death_y_n = death['Date Death'][pd.to_datetime(death['Date Death'], \
                                                                 format='%d/%m/%Y',
                                                                 errors = 'coerce')\
                                                                 .isnull()]

death_disease_case = death['Exact date of death'][~((death['Exact date of death'].isin(['Y','N']))\
                                                      |(death['Exact date of death'].isnull()))]

death['Death Cause'][~pd.to_datetime(\
                                      death['Death Cause'], \
                                      format='%d/%m/%Y', errors = 'coerce')\
                                      .isnull()] = \
                                      death_disease_case

death['Date Death'][pd.to_datetime(\
                                      death['Date Death'], \
                                      format='%d/%m/%Y', errors = 'coerce')\
                                      .isnull()] = \
                                      death_to_date[pd.to_datetime(\
                                                                   death['Date Death'], \
                                                                   format='%d/%m/%Y', errors = 'coerce')\
                                                                   .isnull()]

death['Exact date of death'][~death['Exact date of death'].isin(['Y','N'])] = \
                                  death_y_n[~death['Exact date of death'].isin(['Y','N'])]

death['Death Cause'][pd.to_datetime(\
                                      death['Date Death'], \
                                      format='%d/%m/%Y', errors = 'coerce')\
                                      .isnull()] = \
                                       death_y_n[pd.to_datetime(\
                                      death['Date Death'], \
                                      format='mixed', errors = 'coerce')\
                                      .isnull()]

R:

library(tidyverse)
library(magrittr)
library(anytime)
library(Hmisc)

death_to_date = anytime(death$`Death Cause`) %>% as.character

death_y_n = death$`Date Death`[is.na(as_date(death$`Date Death`))]

death_disease_case = death$`Exact date of death`[death$`Exact date of death` %nin% c('Y','N')]

death$`Death Cause`[!is.na(as_date(death$`Death Cause` ))] = death_disease_case[!is.na(as_date(death$`Death Cause` ))]

death$`Date of Registered Death`[is.na(as_date(death$`Date Death`))] = death_to_date[is.na(as_date(death$`Date Death`))]

death$`Exact date of death`[death$`Exact date of death` %nin% c('Y','N')] = death_y_n[death$`Exact date of death` %nin% c('Y','N')]

但是由于日期格式较多,部分日期格式无法解析成功。有没有一种方法可以在不使用

to_datetime()
/
anytime()
的情况下交换列?

我是Python新手,如果有任何错误,请指出!谢谢你。

python r pandas tidyverse
1个回答
0
投票

日期很棘手,但您可能想查看 lubridate 包。 R 代码会抛出什么错误?如果您遇到错误,我建议您将日期作为字符列开始,获取同一列中的所有数据,然后解析 NA 值。一旦所有内容都采用正确的格式,然后将列强制为日期格式。由于字符列将接受您的更改,因此您将能够执行所需的任何清理操作。第一步强制转换为日期格式的问题在于,通常这些通用转换函数(如

as.date()
)非常挑剔。如果任何一个值的格式错误,整个过程就会失败并抛出错误。因此,如果您陷入其中,请尝试在最后一步中使用 anytime 进行转换,看看是否没有更好的结果。
    

© www.soinside.com 2019 - 2024. All rights reserved.