加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Python > 正文

python – 从特定索引开始的Dataframe cummin列

发布时间:2020-12-20 11:08:54 所属栏目:Python 来源:网络整理
导读:我有两列数据框.日期和十进制数. 我想在数据框中创建一个新列,仅在时间超过9:30时显示十进制数列的cummin 解决方法 使用面具来掩盖,然后使用cummin. # df.index = pd.to_datetime(df.index,errors='coerce')df['cummin'] = df.number.mask(df.index.strftime
我有两列数据框.日期和十进制数.
我想在数据框中创建一个新列,仅在时间超过9:30时显示十进制数列的cummin

enter image description here

解决方法

使用面具来掩盖,然后使用cummin.

# df.index = pd.to_datetime(df.index,errors='coerce')
df['cummin'] = df.number.mask(df.index.strftime('%H:%M') < '09:30').cummin()

您还可以查询索引的小时和分钟属性以获取小时数:

df['cummin'] = df.loc[
    (df.index.hour >= 9) & (df.index.minute > 30),'number'].cummin()

MCVE:

df = pd.DataFrame([1.4,4.5,2.3],index=['9:00','9:31','9:45'],columns=['number'])
df.index = pd.to_datetime(df.index)
df
                     number
2018-12-21 09:00:00     1.4
2018-12-21 09:31:00     4.5
2018-12-21 09:45:00     2.3
df.assign(number=(
    df.number.mask(df.index.strftime('%H:%M') < '09:30').cummin()))

                     number  cummin
2018-12-21 09:00:00     NaN     NaN
2018-12-21 09:31:00     4.5     4.5
2018-12-21 09:45:00     2.3     2.3
df.assign(number=df.loc[
    (df.index.hour >= 9) & (df.index.minute > 30),'number'].cummin())

                     number  cummin
2018-12-21 09:00:00     NaN     NaN
2018-12-21 09:31:00     4.5     4.5
2018-12-21 09:45:00     2.3     2.3

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读