比较用户输入日期和PandasDF日期

yruzcnhs  于 2023-01-07  发布在  其他
关注(0)|答案(2)|浏览(123)

我试图比较用户输入的日期(月份和年份格式)和Pandasdf的日期列,并在df上选择相应的值。
下面是一个例子:

birthDate = input("birth year-month?")
  deathDate = input("death year-month?")

  // assume that user inputs: 2-2022 and 3-2022

  df = pd.DataFrame({'date': ['1-2022', '2-2022', '3-2022'],
                   'value': [1.4, 1223.22, 2323.23]})

   output:
   "birth day value is 1223.22"
   "death day value is 2323.23"
r1zk6ea1

r1zk6ea11#

birthDate = input("birth month-year?")
deathDate = input("death month-year?")

birthday_value = df.loc[df["date"] == birthDate, "value"].values[0]
deathday_value = df.loc[df["date"] == deathDate, "value"].values[0]

print(f"birth day value is {birthday_value}") 
print(f"death day value is {deathday_value}")

输出:

birth day value is 1223.22
death day value is 2323.23
wbgh16ku

wbgh16ku2#

试试这个-

print("birth day value is", float(df[df["date"]==birthDate]["value"]))
print("death day value is", float(df[df["date"]==deathDate]["value"]))

相关问题