'How to add months into a data frame? Python
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)
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
