我试图在插值行之间的数值后每隔第二行更改文本。
stamp value
0 00:00:00 2
1 00:00:00 3
2 01:00:00 5
尝试将此更改应用于第二个图章行(即冒号之间为30而不是00)-str列
stamp value
0 00:00:00 2
1 00:30:00 3
2 01:00:00 5
更改字符串的功能
def time_vals(row):
#run only on odd rows (1/2 hr)
if int(row.name) % 2 != 0:
l, m, r = row.split(':')
return l+":30:"+r
我尝试了以下方法:
hh_weather['time'] =hh_weather[hh_weather.rows[::2]['time']].apply(time_vals(2))
但是出现错误:AttributeError:'DataFrame'对象没有属性'rows'
当我尝试时:
hh_weather['time'] = hh_weather['time'].apply(time_vals)
AttributeError:'str'对象没有属性'name'
有什么想法吗?
答案 0 :(得分:1)
timedelta
代替str
Pandas的优势在于矢量化功能。在这里,您可以使用timedelta
用数字表示时间。如果数据如您的示例所示,即秒始终为零,那么您可以按小时进行累加并增加30分钟。然后有条件地将此系列分配给df['stamp']
。
# convert to timedelta
df['stamp'] = pd.to_timedelta(df['stamp'])
# create series by flooring by hour, then adding 30 minutes
s = df['stamp'].dt.floor('h') + pd.Timedelta(minutes=30)
# assign new series conditional on index
df['stamp'] = np.where(df.index % 2, s, df['stamp'])
print(df)
stamp value
0 00:00:00 2
1 00:30:00 3
2 01:00:00 5
答案 1 :(得分:0)
#convert string value to timedelta (better to work with time)
df['stamp']=pd.to_timedelta(df['stamp'])
#slicing only odd row's from `stamp` column and adding 30 minutes to all the odd row's
odd_df=pd.to_timedelta(df.loc[1::2,'stamp'])+pd.to_timedelta('30 min')
#updating new series (out_df) with the existing df, based on index.
df['stamp'].update(odd_df)
#print(df)
stamp value
0 00:00:00 2
1 00:30:00 3
2 01:00:00 5