How do I add a column df1.days
that uses max(df2.days) + 1
(1175) and will incrementally add one for all remaining rows?
I cannot use cumsums for this.
df1 = pd.DataFrame({
'date': ['2001-01-01','2001-01-02','2001-01-03', '2001-01-04', '2001-01-05'],
'prod': [800, 900, 1200, 700, 600]})
df2 = pd.DataFrame({'days': [99,1,188,1173,1174]})
Desired dataframe:
print(d1)
date prod days
0 2001-01-01 800 1175
1 2001-01-02 900 1176
2 2001-01-03 1200 1177
3 2001-01-04 700 1778
4 2001-01-05 600 1779
pandas
method Using fillna
and cumsum
:
df1.loc[0, 'days'] = df2['days'].max()
df1['days'] = df1['days'].fillna(1).cumsum()
date prod days
0 2001-01-01 800 1175.0
1 2001-01-02 900 1176.0
2 2001-01-03 1200 1177.0
3 2001-01-04 700 1178.0
4 2001-01-05 600 1179.0
numpy
method df1['days'] = np.cumsum(np.append([df2['days'].max()], np.ones(len(df1)-1)))
date prod days
0 2001-01-01 800 1175.0
1 2001-01-02 900 1176.0
2 2001-01-03 1200 1177.0
3 2001-01-04 700 1178.0
4 2001-01-05 600 1179.0
cumsum
with for loop
(not preferrable)
values = []
value = df2['days'].max()
for x in range(len(df1)):
values.append(value)
value += 1
df1['days'] = values
date prod days
0 2001-01-01 800 1175
1 2001-01-02 900 1176
2 2001-01-03 1200 1177
3 2001-01-04 700 1178
4 2001-01-05 600 1179
You can insert a column with range =(df2 days max+1 to df2 days max+number of rows). The first parameter len(df1.columns) is to add this column in the end, doesn't really affect the logic
df1.insert(len(df1.columns),'days',range(df2.days.max()+1, df2.days.max()+1+len(df1)))
date prod days
0 2001-01-01 800 1175
1 2001-01-02 900 1176
2 2001-01-03 1200 1177
3 2001-01-04 700 1178
4 2001-01-05 600 1179
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.