I have the following df:
JavaScript
x
7
1
ID data
2
0 123123 1
3
1 123412 1
4
2 123123 1
5
3 234234 2
6
4 432424 2
7
What I want to do know is add x new rows per row based on the id. So more specific spoken, I want to add a new column containing the date from a range of 7 days and then add a new row with the date for every ID in the df.
so the output should look sth like this if we say I want only dates in the range of 2 days:
JavaScript
1
12
12
1
ID data date
2
0 123123 1 2020-02-01
3
0 123123 1 2020-02-02
4
1 123412 1 2020-02-01
5
1 123412 1 2020-02-02
6
2 123123 1 2020-02-01
7
2 123123 1 2020-02-02
8
3 234234 2 2020-02-01
9
3 234234 2 2020-02-02
10
4 432424 2 2020-02-01
11
4 432424 2 2020-02-02
12
I thought of doing is with loops, however this idea seems really slow and not efficient.
JavaScript
1
5
1
df = pd.DataFrame(columns=['ID','data','date'])
2
for date in pd.date_range(start=dateRange[0],end=dateRange[1]):
3
for row in df_old:
4
df = df.append({'ID': row['ID'], 'data': row['data'], 'date': date}, ignore_index=True)
5
I am really thankful for any better idea
Advertisement
Answer
Let us try assign
with melt
JavaScript
1
13
13
1
out = df.assign(**{'date1':'2020-02-01','date2':'2020-02-02'}).melt(['ID','data']).sort_values('ID')
2
ID data variable value
3
0 123123 1 date1 2020-02-01
4
2 123123 1 date1 2020-02-01
5
5 123123 1 date2 2020-02-02
6
7 123123 1 date2 2020-02-02
7
1 123412 1 date1 2020-02-01
8
6 123412 1 date2 2020-02-02
9
3 234234 2 date1 2020-02-01
10
8 234234 2 date2 2020-02-02
11
4 432424 2 date1 2020-02-01
12
9 432424 2 date2 2020-02-02
13