Loop only Saving Last DataFrame on csv

I have the below loop, I would like it to save all the dataframes into a csv as it loops but it only save the last loop. I am a newbee please advise where am I going wrong.

inverterdata=[]
for i in invertersinfo.items():
    inverterdata.append([i[0],i[1]])
    

    invdata=pd.DataFrame(inverterdata)
    data = pd.json_normalize(json.loads(invdata.to_json(orient="records")))
    hh=data.explode('1.telemetries')                         
    data2 = pd.json_normalize(json.loads(hh.to_json(orient="records")))  
    data2 = data2.drop(['1.count', '0'], axis=1)
    #data2.columns = data2.columns.str.replace(r'1.telemetries.', '')
    data2.columns = [col.replace('1.telemetries.', '') for col in data2.columns]
    data2.columns += "_"+ name
    
             
data2.to_csv("Samapledata.csv", index=False).

This is a little hard to answer without really seeing some sample data since there is a lot going on here. It appears you’re transforming and dropping some columns, but without seeing the data its a bit hard for me to suggest other ways to do it. Can you share a little bit of the sample input and what you would like the output to look like?

Also, if you haven’t seen it, the Pandas documentation has some really good examples of ways to achieve similar things. Particularly the section on column selection and deletion and Dataframe.rename().