我正在读取日期时间格式的字符串。然而,毫秒的位数不是 6 位,而是只有 3 位,末尾带有字母 Z。如何读取该字符串并将其设为日期时间对象,添加 1 天,然后按上述格式将其写为字符串,即 3 位数字表示毫秒,末尾有一个字母 Z。我尝试了以下代码但没有成功:
old_date= "2018-06-06T23:59:59.999Z"
new_date = datetime.datetime.strptime(old_date, '%Y-%m-%d %H:%M:%S.%f%Z') + datetime.timedelta(days=1)
print(new_date)
Run Code Online (Sandbox Code Playgroud) 我有一个 Pandas 数据框对象。我想从现有列的子字符串创建新列。我的数据如下所示:
Date variable want1 want2 want3
0 02-01-08 Australia - Sydney - A Australia Sydney A
1 03-01-08 Australia - Sydney - A Australia Sydney A
2 04-01-08 Australia - Sydney - A Australia Sydney A
3 05-01-08 Canada - Toronto - B Canada Toronto B
4 06-01-08 Canada - Toronto - B Canada Toronto B
Run Code Online (Sandbox Code Playgroud)
哪里want1来want3的我需要什么。
这可能是一个简单的问题,但我无法让它发挥作用。我想创建一个列varNew,等于一列的一半Price,如果只有一个列var1,并var2具有1的值,var1并且var2只需要0或1。如果两个数值var1和var2是1,那么varNew = Price。所以想要的数据看起来像这样
var1 var2 Price varNew
0 0 10 10
0 1 14 7
1 0 12 6
1 1 20 20
Run Code Online (Sandbox Code Playgroud)
我试过:
update varNew:?[((var1+var2)>0);Price%2;Price] from table 和
varNew:?[(var1=1 and var2=0)|(var1=0 and var2=1);Price%2;Price] from table 和
varNew:?[var1=1 or var2=1;Price%2;Price] from table
但它们没有按预期工作。特别是,它返回 12 而不是 6。这是怎么回事?为什么这些条件不起作用?我该怎么做?