Home > OS >  How to create repetitive values column (using hours) for ID numbers for each date available?
How to create repetitive values column (using hours) for ID numbers for each date available?

Time:08-25

In the following example of a data frame:

Date ID number
15/06/2022 1523
15/06/2022 1402

I'm trying to add a column in pandas to a the data frame that has repetitive values, such as:

Date ID number Hour
15/06/2022 1523 1
15/06/2022 1523 2
15/06/2022 1523 4
15/06/2022 1523 5
15/06/2022 1523 6
15/06/2022 1523 7
15/06/2022 1523 8
15/06/2022 1402 1
15/06/2022 1402 2
15/06/2022 1402 4
15/06/2022 1402 5
15/06/2022 1402 6
15/06/2022 1402 7
15/06/2022 1402 8

How is this possible please? Thanks in advance

CodePudding user response:

You can multiply and concat the df

pd.concat([df]*8).sort_values('ID number')

         Date  ID number
1  15/06/2022       1402
1  15/06/2022       1402
1  15/06/2022       1402
1  15/06/2022       1402
1  15/06/2022       1402
1  15/06/2022       1402
1  15/06/2022       1402
1  15/06/2022       1402
0  15/06/2022       1523
0  15/06/2022       1523
0  15/06/2022       1523
0  15/06/2022       1523
0  15/06/2022       1523
0  15/06/2022       1523
0  15/06/2022       1523
0  15/06/2022       1523

CodePudding user response:

Try:

df.reindex(df.index.repeat(8)).assign(hour=np.tile(np.arange(1,9),2))

Output:

         Date  ID number  hour
0  15/06/2022       1523     1
0  15/06/2022       1523     2
0  15/06/2022       1523     3
0  15/06/2022       1523     4
0  15/06/2022       1523     5
0  15/06/2022       1523     6
0  15/06/2022       1523     7
0  15/06/2022       1523     8
1  15/06/2022       1402     1
1  15/06/2022       1402     2
1  15/06/2022       1402     3
1  15/06/2022       1402     4
1  15/06/2022       1402     5
1  15/06/2022       1402     6
1  15/06/2022       1402     7
1  15/06/2022       1402     8

And, you can add .reset_index() to get unique indexing for the rows also.

  • Related