正如有人建议的那样,我给出了一个可验证的例子。如果您从中取出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()传递。我觉得我错过了一些非常明显的东西,但也许不是。另外,如果有人有更好的方法来做这一切,我总是学习如何更有效。你知道吗
将这些值中的每一个都转换为datetime对象,然后执行if/elif过滤要容易得多。你知道吗
你也可以考虑其中的
datetime.now()
。你知道吗希望对你有所帮助,它会让你的流量更好。你知道吗
为最小的示例兼容性而编辑
您可以定义一个函数来计算一个人的年龄:
将此函数沿行应用于数据框:
它又回来了pd系列如果没有遗漏数据,所有人的年龄以年为单位。您可以将其连接到数据帧:
然后添加另一个状态栏并打印结果:
如果您不喜欢复制粘贴日期选择,请将其替换为来自Andrey Portnoy's解决方案的
datetime
方法。你知道吗Pandas对时间序列有极好的支持,因此利用适当的工具是个好主意。将列转换为单个Datetime列后,可以对其执行时间算术:
编辑:请参阅@ALollz下面关于时间戳规范化的讨论。你知道吗
相关问题 更多 >
编程相关推荐