简体   繁体   English

如何在带有小时列的Pandas数据框中“丰富”每条记录?

[英]How do I “enrich” every record in a Pandas dataframe with an hour column?

I have some dataframe in Pandas: 我在Pandas中有一些数据框:

 1   2
 a  .2
 a  .3
 b  .5

I would like to add, for each of those records, a column with hour (from 0 to 23), so it will look like 我想为每个记录添加一个带有小时(从0到23)的列,因此它看起来像

 1   2    3
 a  .2    0
 a  .2    1
 a  .2    2
...
 a  .2   23
 a  .3    0
 a  .3    1
...
 a  .3   23
 b  .5    0
...
 b  .5   23

Create the hours array: 创建小时数组:

import numpy as np
hours = np.tile(np.arange(24), len(df))

Repeat each record of df by 24 times: df的每个记录重复24次:

df = df.loc[df.index.repeat(24)].reset_index(drop=True)

Assign the hours array as a new column to the data frame: 小时数组分配为数据框的新列:

df[3] = hours

df.head()
#   1     2 3
#0  a   0.2 0
#1  a   0.2 1
#2  a   0.2 2
#3  a   0.2 3
#4  a   0.2 4

Put together: 放在一起:

def expand_hours(df):
    import numpy as np
    hours = np.tile(np.arange(24), len(df))
    df = df.loc[df.index.repeat(24)].reset_index(drop=True)
    df[3] = hours
    return df

If your DataFrame is called df try this: 如果您的DataFrame称为df尝试以下操作:

df['hour'] = Series(np.random.randint(0,24), index=df.index)

This should add a column with name 'hour' filled with integers generated between 0 and 23. 这应添加一个名称为“ hour”的列,其中填充了介于0到23之间的整数。

暂无
暂无

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

相关问题 如何将熊猫数据框中的每一列添加到列表中,除了第一列? - How do I add every column in a pandas dataframe to a list except for the first column? 如何将小时添加到熊猫数据框列 - how to add hour to pandas dataframe column 如何向 pandas dataframe 添加一列,该列在一个范围内具有最高值但将其应用于每一行? - How do I add a column to a pandas dataframe which has the highest value in a range but applying it to every row? 如何为 Pandas 数据框中的列表列中的每个元素添加双引号? - How do I add double quotes to every element in a list column in a pandas dataframe? 如何为 Pandas 数据框列中的每个唯一值添加重复的月份行? - How do I add repeated month rows for every unique value in a pandas dataframe column? 如何每隔 n 小时从 Pandas DataFrame 获取值? - How can I get the values at every nth hour from a Pandas DataFrame? 使用 Pandas DataFrame,如何保留每 7 行? - With a pandas DataFrame, how do I keep every 7th row? 如何将列添加到特定小时的滚动平均值的 pandas dataframe - how to add a column to a pandas dataframe of the rolling average of specific hour 如何在 python 中使用 Pandas 数据框按特定日期和小时进行过滤 - How do I filter by a certain date and hour using Pandas dataframe in python 如何对每列都有一个系列的 DataFrame 进行操作? - How do I operate on a DataFrame with a Series for every column?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM