[英]Inserting rows in dataframe based on previous rows
I need to insert rows based on the column week, in some cases i have missing weeks in the middle of the dataframe and i want to insert rows to fill in the missing rows as copies of the last existing row, in this case copies of week 8 but with incremental value for the column week : on this table you can see the jump from week 8 to 12我需要根据列周插入行,在某些情况下,我在数据框中间缺少周,我想插入行以填充缺失的行作为最后一行的副本,在这种情况下为周的副本8 但列周的增量值:在此表上,您可以看到从第 8 周到第 12 周的跳跃
the perfect output would be as follow: the final table with incremental values in column week the correct way完美的输出如下:最终表中列周的增量值正确的方式
Below is the code i have, it inserted only one row which is 11下面是我的代码,它只插入了一行,即 11
for f in range(1, 52 , 1):
if final.iat[i,8]== f and final.iat[i-1,8] != f-1 :
if final.iat[i,8] > final.iat[i-1,8] and final.iat[i,8] != (final.iat[i-1,8] - 1):
line = final.iloc[i-1]
c1 = final[0:i]
c2 = final[i:]
c1.loc[i]=line
concatinated = pd.concat([c1, c2])
concatinated.reset_index(inplace=True)
concatinated.iat[i,11] = concatinated.iat[i-1,11]
concatinated.iat[i,9]= f-1
finaltemp = finaltemp.append(concatinated)```
Build the full list of weeks and use pd.merge
then fill forward to replace NaN
:构建完整的周列表并使用
pd.merge
然后向前填充以替换NaN
:
weeks = range(df['Week'].min(), df['Week'].max()+1)
out = pd.merge(df, pd.Series(weeks, name='Week'), how='right').ffill()
>>> out
index type Project Week Cumulated Hours Remaining hours
0 XXY 1.0 A 7 18.0 2000.0
1 XXY 1.0 A 8 20.0 1900.0
2 XXY 1.0 A 9 20.0 1900.0
3 XXY 1.0 A 10 20.0 1900.0
4 XXY 1.0 A 11 20.0 1900.0
5 XXY 1.0 A 12 24.0 1500.0
6 XXY 1.0 A 13 36.0 1400.0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.