计算活人与死人的年龄

2024-09-28 01:29:51 发布

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

编辑:

正如有人建议的那样,我给出了一个可验证的例子。如果您从中取出pandas并简单地放置原始值而不是数据帧值,那么它就可以完美地工作。你知道吗

如果你把熊猫带回来,就像我下面所说的,程序运行并返回0进行打印(真年龄)。你知道吗

import pandas as pd
import numpy as np
from datetime import datetime

data = np.array([['','bornYear','bornMonth', 'bornDay','diedYear','diedMonth','diedDay'],
                ['Record1',1932,8,17,1980,3,22],
                ['Record2',1950,4,12,1980,3,22]])


df = pd.DataFrame(data=data[1:,1:],
                  index=data[1:,0],
                  columns=data[0,1:])

byear = int(df.iloc[1]['bornYear'])
bmonth = int(df.iloc[1]['bornMonth'])
bday = int(df.iloc[1]['bornDay'])
died_year = df.iloc[1]['diedYear']
died_month = df.iloc[1]['diedMonth']
died_day = df.iloc[1]['diedDay']
now_year = datetime.now().year
now_month = datetime.now().month
now_day = datetime.now().day
age_raw = now_year - byear
true_age = 0


if died_year is not None:
    died_year = int(died_year)
    died_month = int(died_month)
    died_day = int(died_day)

    age_raw = float(died_year) - float(byear)

    if bmonth > died_month:
        if bday > died_day:
            true_age = age_raw - 1
        elif bday < died_day:
            true_age = age_raw
    elif bmonth < died_month:
        true_age = age_raw

print(true_age)

原始帖子:

所以,我得到了一个pandas数据帧,它是一个MySQL查询的结果,该查询搜索一个人的名字,然后返回关于他们的一些信息。其中一个信息就是他们的年龄。这张桌子上有活人和死人。我试着这样做,如果一个人死了,它使用他们的实际年龄(在死亡时),而不是他们的年龄,如果他们还活着。如果他们还活着,死亡日期字段是空的;如果他们死了,这些字段当然有值。以下是我声明的相关变量:

bmonth = int(storage.iloc[0]['birthMonth'])
bday = int(storage.iloc[0]['birthDay'])
byear = int(storage.iloc[0]['birthYear'])
died_year = storage.iloc[0]['deathYear']
died_month = storage.iloc[0]['deathMonth']
died_day = storage.iloc[0]['deathDay']
now_year = datetime.now().year
now_month = datetime.now().month
now_day = datetime.now().day
age_raw = now_year - byear
true_age = 0

现在,我把它设计成嵌套的if语句,但我在某些地方出错了。如果这个人还活着,一切正常;当我打印年龄时,它会输出正确的年龄。但是,如果此人已死亡,则打印的年龄始终为零。下面是嵌套的if语句以及相关的print语句:

#Here are the nested if statements:

    if died_year is None:   

            if bmonth > now_month:
                    if bday > now_day:
                            true_age = age_raw - 1
                    elif bday < now_day:
                            true_age = age_raw
            elif bmonth < now_month:

                    true_age = age_raw

    elif died_year is not None:

            died_year = int(died_year)
            died_month = int(died_month)
            died_day = int(died_day)

            age_raw = died_year - byear

            if bmonth > died_month:
                    if bday > died_day:
                            true_age = age_raw - 1
                    elif bday < died_day:
                            true_age = age_raw
            elif bmonth < died_month:
                    true_age = age_raw

#And now the print statement:

print("DOB: "+str(bmonth)+"/"+str(bday)+"/"+str(byear)+" ("+str(true_age)+" years old)")

此外,我还准备了以下内容,以便在人员死亡时在输出中返回死亡日期。它工作正常,返回正确的日期,因此我知道值都是正确的:

    if died_year is not None:
            print("*DECEASED: "+str(died_month)+"/"+str(died_day)+"/"+str(died_year))

注意在满足适当的条件之前,我没有将变量died\u year、died\u month和died\u day转换为整数;在if语句之外这样做会触发错误,因为null值不能作为int()传递。我觉得我错过了一些非常明显的东西,但也许不是。另外,如果有人有更好的方法来做这一切,我总是学习如何更有效。你知道吗


Tags: trueagedatetimerawifyearnowint
3条回答

将这些值中的每一个都转换为datetime对象,然后执行if/elif过滤要容易得多。你知道吗

import datetime
bmonth = int(storage.iloc[0]['birthMonth'])
bday = int(storage.iloc[0]['birthDay'])
byear = int(storage.iloc[0]['birthYear'])

died_year = storage.iloc[0]['deathYear']
died_month = storage.iloc[0]['deathMonth']
died_day = storage.iloc[0]['deathDay']

start = datetime.datetime(month = bmonth, day=bday, year=byear)
end =  datetime.datetime(month=died_month, day=died_day, year=died_year)
(start-end).days#returns the difference between the days

你也可以考虑其中的datetime.now()。你知道吗

希望对你有所帮助,它会让你的流量更好。你知道吗

为最小的示例兼容性而编辑

您可以定义一个函数来计算一个人的年龄:

from datetime import date 


def calc_age(row):
    bm = row['bornMonth']
    bd = row['bornDay']
    by = row['bornYear']

    dm = row['diedMonth']
    dd = row['diedDay']
    dy = row['diedYear']

    birth_date = date(*[int(i) for i in (by, bm, bd)])  # suppose that all the parameters is not None
    try:
        end_date = date(*[int(i) for i in (dy, dm, dd)])
    except (TypeError, ValueError):  # if death date is None
        end_date = date.today()

    # is birth date after death date or today; if True == 1, else == 0
    is_next_year = ((end_date.month, end_date.day) < (birth_date.month, birth_date.day))
    age = end_date.year - birth_date.year - is_next_year 
    return age

将此函数沿行应用于数据框:

df.apply(calc_age, axis=1)

它又回来了pd系列如果没有遗漏数据,所有人的年龄以年为单位。您可以将其连接到数据帧:

df['personsAge'] = df.apply(calc_age, axis=1)

然后添加另一个状态栏并打印结果:

def is_dead(row):
    dm = row['diedMonth']
    dd = row['diedDay']
    dy = row['diedYear']
    try:
        died = date(*[int(i) for i in (dy, dm, dd)])
        return True
    except ValueError:
        return False

df['is_dead'] = df.apply(is_dead, axis=1)

def print_status(row):
    bm = row['bornMonth']
    bd = row['bornDay']
    by = row['bornYear']

    dm = row['diedMonth']
    dd = row['diedDay']
    dy = row['diedYear']
    age = row['personsAge']

    print("DOB: "+str(bm)+"/"+str(bd)+"/"+str(by)+" ("+str(age)+" years old)")
    if row['is_dead']:
        print("*DECEASED: "+str(dm)+"/"+str(dd)+"/"+str(dy))

df.apply(print_status, axis=1)

stdout:
DOB: 8/17/1932 (47 years old)
*DECEASED: 3/22/1980
DOB: 4/12/1950 (68 years old)

如果您不喜欢复制粘贴日期选择,请将其替换为来自Andrey Portnoy's解决方案的datetime方法。你知道吗

Pandas对时间序列有极好的支持,因此利用适当的工具是个好主意。将列转换为单个Datetime列后,可以对其执行时间算术:

# demo dataframe
df = pd.DataFrame({
    'birthMonth': [5, 2],
    'birthDay': [4, 24],
    'birthYear': [1924, 1997],
    'deathMonth': [3, None],
    'deathDay': [1, None],
    'deathYear': [2008, None]
})

# convert birth dates to datetimes
birth = pd.to_datetime(df[['birthMonth', 'birthDay', 'birthYear']]
                       .rename(columns={'birthMonth': 'month', 'birthDay': 'day', 'birthYear': 'year'}))
# convert death dates to datetimes
death = pd.to_datetime(df[['deathMonth', 'deathDay', 'deathYear']]
                       .rename(columns={'deathMonth':'month', 'deathDay': 'day', 'deathYear': 'year'}))

# calculate age in days, normalizing 'now' to midnight of today
age = (pd.Timestamp.now().normalize() - birth).where(death.isnull(), other=death-birth)

编辑:请参阅@ALollz下面关于时间戳规范化的讨论。你知道吗

相关问题 更多 >

    热门问题