I want to drop a row in dataframe in loop/iteration after I use the row for some calculation and generate new rows , here is code for the iteration:
for i,row in df_1_filtered.iterrows():
if row.notnull()['Slutdato']:
date_range =row['Slutdato']-row['Indrykningsdato']
for d in range(date_range.days+1):
#generate rows with data from the row
df_temp=pd.DataFrame(data=d_t)
df_1_filtered=df_1_filtered.append(df_temp)
# if i dont have drop, the dataframe will have the row and generated rows
df_1_filtered.drop(df_1_filtered.index[i], inplace=True)
If I put df_1_filtered.drop(df_1_filtered.index[i], inplace=True)
befor the inner for loop, I will get a empty file , if i put df_1_filtered.drop(df_1_filtered.index[i], inplace=True)
after the for loop. It remove all the rows that I have generate in the loop. Why is like that, what is the correct way to drop the row
I ended up with create a empty df and use if else to append all the rows.
df_spread_enddate=pd.DataFrame()
for i,row in df_1_filtered.iterrows():
if row.notnull()['Slutdato']:
date_range =(row['Slutdato']-row['Indrykningsdato']).days+1
b_avg=row['Bruttopris']/date_range
n_abg=row['Net-net kunde']/date_range
for d in range(date_range):
#generate rows with data from the row
df_temp=pd.DataFrame(data=d_t)
df_spread_enddate=df_spread_enddate.append(df_temp)
#df_1_filtered=df_1_filtered.drop(df_1_filtered.iloc[i:], inplace=True)
else:
df_spread_enddate=df_spread_enddate.append(row)
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.