I’m trying to build a dataframe using for loop, below start works perfectly:
JavaScript
x
9
1
import pandas as pd
2
df = pd.DataFrame(columns=['DATE1', 'SLS_CNTR_ID'])
3
4
for i in range(2):
5
this_column = df.columns[i]
6
df[this_column] = [i, i+1]
7
8
df
9
And I got the correct one:
Then I tried to make my implemetation as below:
JavaScript
1
14
14
1
import pandas as pd
2
df = pd.DataFrame(columns=['DATE1', 'SLS_CNTR_ID'])
3
4
SLS = [58, 100]
5
6
row = 0
7
for _, slc in enumerate(SLS):
8
for single_date in daterange(start_date, end_date):
9
df[row] = [single_date.strftime("%Y-%m-%d"), slc]
10
row = row + 1
11
12
print(type(row), type(df))
13
df
14
But the result I got was a horizontal dataframe, not a vertical one
Even the data in the main hedears got posted as NAN
?
I tried using enforced header type declaration, but gave same result:
JavaScript
1
26
26
1
import pandas as pd
2
import numpy as np
3
#Create empty DataFrame with specific column names & types
4
# Using NumPy
5
dtypes = np.dtype(
6
[
7
('DATE1',np.datetime64),
8
('SLS_CNTR_ID', int),
9
]
10
)
11
df = pd.DataFrame(np.empty(0, dtype=dtypes))
12
#df = pd.DataFrame(columns=['DATE1', 'SLS_CNTR_ID'])
13
14
print(df)
15
16
SLS = [58, 100]
17
18
row = 0
19
for _, slc in enumerate(SLS):
20
for single_date in daterange(start_date, end_date):
21
df[row] = [single_date.strftime("%Y-%m-%d"), slc]
22
row = row + 1
23
24
print(type(row), type(df))
25
df
26
Advertisement
Answer
Use df.loc[row]
instead of df[row]
to set the rows.
Though I’d rather implement this using a merge instead of the loops:
JavaScript
1
3
1
(pd.DataFrame({"DATE1": pd.date_range("2020-01-01", "2020-02-01")})
2
.merge(pd.Series(SLS, name="SLS_CNTR_ID"), how="cross"))
3
Or leverage itertools
to obtain the cross-product:
JavaScript
1
22
22
1
import itertools
2
3
dates = pd.date_range("2020-01-01", "2020-02-01")
4
SLS = [58, 100]
5
6
pd.DataFrame(itertools.product(SLS, dates), columns=["SLS_CNTR_ID", "DATE1"])
7
8
SLS_CNTR_ID DATE1
9
0 58 2020-01-01
10
1 58 2020-01-02
11
2 58 2020-01-03
12
3 58 2020-01-04
13
4 58 2020-01-05
14
..
15
59 100 2020-01-28
16
60 100 2020-01-29
17
61 100 2020-01-30
18
62 100 2020-01-31
19
63 100 2020-02-01
20
21
[64 rows x 2 columns]
22