简体   繁体   English

Pandas/Numpy - 如何在与索引长度匹配的数据帧列中添加重复出现的整数

[英]Pandas/Numpy - how to add reoccurring integers in a dataframe column that matches the length of the index

I have a pandas dataframe with several columns and 720 rows.我有一个包含多列和 720 行的 Pandas 数据框。 I want to add a column with a reoccurring range of numbers (0 to 23) to this dataframe.我想向这个数据框添加一个重复出现的数字范围(0 到 23)的列。 First row being 0, twenty third row being 23, twentyfourth row is 0, etc.第一行为 0,第三行第二十为 23,第二十四行为 0,以此类推。

I tried this:我试过这个:

for i in range(24):
    print(i)

df['hours(integers)']= np.ones(720) * (i)

However, it did not work.但是,它不起作用。 every new row became 23.每一个新行都变成了 23。

How can I make this reoccuring integer column?如何制作这个重复出现的整数列?

Use modulo 24 , no loops are necessary here:使用modulo 24 ,这里不需要循环:

df = pd.DataFrame({'a':1}, index=range(720))

#default Range index
df['hours(integers)'] = df.index % 24
#any index with helper array
#df['hours(integers)'] = np.arange(len(df)) % 24

print (df.head(30))
    a  hours(integers)
0   1                0
1   1                1
2   1                2
3   1                3
4   1                4
5   1                5
6   1                6
7   1                7
8   1                8
9   1                9
10  1               10
11  1               11
12  1               12
13  1               13
14  1               14
15  1               15
16  1               16
17  1               17
18  1               18
19  1               19
20  1               20
21  1               21
22  1               22
23  1               23
24  1                0
25  1                1
26  1                2
27  1                3
28  1                4
29  1                5  

您可以在 numpy 中使用 tile 功能

df['hours(integers)'] = np.tile(list(range(24)), 30)

暂无
暂无

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

相关问题 Pandas,如何将系列添加到 DataFrame 列,其中系列索引与 DataFrame 列匹配? - Pandas, how to add Series to DataFrame column, where series index matches a DataFrame column? Python-将numpy数组作为列添加到具有不同长度的pandas数据帧 - Python - add a numpy array as column to a pandas dataframe with different length Pandas:将系列添加到数据框作为列(相同的索引,不同的长度) - Pandas: Add series to dataframe as a column (same index, different length) Pandas (python):如何将列添加到数据帧以进行索引? - Pandas (python): How to add column to dataframe for index? 如何在 Pandas 数据框中为索引添加列名 - How to add column name for index in Pandas dataframe Pandas:需要从numpy数组中添加一个新列,但是长度比数据帧的长度长 - Pandas: need to add a new column from a numpy array, but the length is longer than the dataframe's length 如何使用pandas数据框的列值更改numpy数组的索引值 - how to change the index value of numpy array with column values of pandas dataframe 如何将numpy日期时间列表添加到Pandas Dataframe中作为列? - How to add numpy datetime list into Pandas Dataframe as column? 当列名是整数时,按列号索引 Pandas DataFrame - Index pandas DataFrame by column numbers, when column names are integers 如何将 pandas Dataframe 的索引设置为列长度的索引? - How to set the index of a pandas Dataframe to that of the length of the Columns?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM