字符串和日期条件

时间:2018-02-22 15:05:13

标签: python

data = [{'A':"re", "B": 120630, "C": 120630, "D": "Sunday"}, {'A':"aeg", "B": 121228, "C": 130104, "D": "Monday"}, {'A':"aab", "B": 120601, "C": 120608, "D": "Sunday"}, {'A':"bbe", "B": 120925, "C": 120630, "D": "Monday"}, {'A':"feh", "B": 120726, "C": 120803, "D": "Monday"}, {'A':"dd", "B": 120622, "C": 120630, "D": "Monday"}]
df = pd.DataFrame(data)
df["B"] = pd.to_datetime (df["B"], format="%y%m%d")
df["C"] = pd.to_datetime (df["C"], format="%y%m%d")

我想选择具有两种条件的行:

  • D栏是“星期一”。
  • B列日期是 C列日期之前的7天或8天。

我的意思是,我想获得这些行:

aeg 2012-12-28 2013-01-04  Monday

feh 2012-07-26 2012-08-03  Monday

dd  2012-06-22 2012-06-30  Monday

我试过了:

df[(df["D"] == "Monday") & (df["B"] < df["C"])]

但此代码不满足第二个条件( B列日期是 C列日期之前7或8天)。

还有其他想法吗? 谢谢。

1 个答案:

答案 0 :(得分:1)

这是一种方式。

var solution = 10;
var n = prompt("Enter a number");
var i = 1;

while ((i < 5) && (n != solution)) {
  console.log(n + " is too " + ((n < solution) ? "small" : "big"));
  n = prompt("Try again");
  i++;
}

if (n == solution) {
  console.log("Good job, the solution was " + n + " you got that in " + i + " tries")
}
else {
  console.log( n + " is too " + ((n < solution) ? "small" : "big"));
  console.log(" You lost, your " + i + " tries have ended")
}