繁体   English   中英

熊猫将功能应用于列的第二行

[英]Pandas apply function to every second row of column

我试图在行之间的数值插值后更改第二行上的文本。

    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”

有任何想法吗?

使用timedelta代替str

熊猫的优势在于矢量化功能。 在这里,您可以使用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
#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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM