Python 将Pandas系列的上一个值与字符串和整数混合使用
我想用那天的日期来覆盖时间。此列表大约有100行,下面是一个示例:Python 将Pandas系列的上一个值与字符串和整数混合使用,python,regex,pandas,Python,Regex,Pandas,我想用那天的日期来覆盖时间。此列表大约有100行,下面是一个示例: Date 0 May-21-20 #Gets passed 1 02:51PM #(should read May-21-20) 2 01:59PM #(should read May-21-20) 3 01:29PM #etc 4 12:45PM #etc 5 12:42PM 6 11:55AM 7 10:02AM 8 09:37AM #(should read May-21-20)
Date
0 May-21-20 #Gets passed
1 02:51PM #(should read May-21-20)
2 01:59PM #(should read May-21-20)
3 01:29PM #etc
4 12:45PM #etc
5 12:42PM
6 11:55AM
7 10:02AM
8 09:37AM #(should read May-21-20)
9 May-20-20 #gets passed
10 02:47PM #(should read May-20-20)
11 02:30PM #(should read May-20-20)
12 02:29PM #(should read May-20-20)
13 02:01PM #(should read May-20-20)
以下是我目前的代码:
for i in headline_table['Date']:
date_list = headline_table['Date'].tolist() #Make the pd Sereies a List
index_value = date_list.index(i) #Now a list so I can reference index value
previous = index_value - 1 #index of current minus one = previous value
if re.search(r'^[A-Z]', i):
pass
else:
headline_table['Date'][i] = headline_table.loc[previous, 'Date']
我尝试了很多不同的方法来解决这个问题,但似乎无法解决。我没有发现代码有任何错误,但是时间没有被日期覆盖,相反,似乎什么都没有发生。我们可以在
中使用ffill
df['Date1']=df.Date.where(df.Date.str.contains('-')).ffill()
df.assign(dates=df.Date.str.extract((\A[A-zA-Z].*)).ffill().drop('Date',axis=1)
?