Home > Mobile >  How to add months into a data frame? Python
How to add months into a data frame? Python

Time:02-26

I'd like to add a column into my data frame, the final version that I'd like to have is as follows,

   Plant  Year Month
0      A  2021   1
1      B  2021   1
2      C  2021   1
3      A  2021   2
4      B  2021   2
5      C  2021   2
6      A  2021   3
7      B  2021   3
8      C  2021   3
9      A  2021   4
10     B  2021   4
11     C  2021   4

and the code is as follows,

import pandas as pd
import numpy as np

TotalMonth = [1,2,3,4]
df = pd.DataFrame()
Plants = ['A', 'B', 'C']

a = list(range(0, 4))
for m in a:
    q = range(0, len(Plants))
    for p in q:
        df = df.append({'Plant': Plants[p]}, ignore_index=True)
        df['Year'] = 2021

print(df)

CodePudding user response:

Try adding this line:

df['Month'] = sorted(TotalMonth*3)

Output:

print(df)

   Plant  Year  Month
0      A  2021      1
1      B  2021      1
2      C  2021      1
3      A  2021      2
4      B  2021      2
5      C  2021      2
6      A  2021      3
7      B  2021      3
8      C  2021      3
9      A  2021      4
10     B  2021      4
11     C  2021      4

Explanation: by doing TotalMonth*3 you get a list that is a three-fold repetition of the original one, then you sort it using sorted so that all numbers are in ascending order, then you assign it to the column.

I also suggest a way to get the same result in much fewer lines of code:

df = pd.DataFrame()
df['Plant'] = Plants*4
df['Year'] = 2021
df['Month'] = sorted(TotalMonth*3)

print(df)

   Plant  Year  Month
0      A  2021      1
1      B  2021      1
2      C  2021      1
3      A  2021      2
4      B  2021      2
5      C  2021      2
6      A  2021      3
7      B  2021      3
8      C  2021      3
9      A  2021      4
10     B  2021      4
11     C  2021      4
  • Related