Home > Net >  Save csv file in each iteration of loop python
Save csv file in each iteration of loop python

Time:09-05

I want to know how can i save each CSV file in iteration on a pandas data frame.

Like i need to save each file as newcsv_1, newcsv_2, newcsv_3 .....

I have used it for loop but it is starting saving from 0 ex:

for i in range(0, len(arr)):
    df = pd.Dataframe(arr[i])

    df.to_csv("newcsv_{}".format(i))

newcsv_0, newcsv_1, newcsv_2

but I do not want to save it from 0 want to save it from 1

CodePudding user response:

for i in range(1, len(arr)   1):
    df = pd.Dataframe(arr[i])

    df.to_csv("newcsv_{}".format(i))

CodePudding user response:

Just do:

df.to_csv("newcsv_{}.csv".format(i 1))

A better way is to:

df.to_csv(f"newcsv_{i 1}.csv") # f-string format python 3

CodePudding user response:

df.to_csv("newcsv_{}.csv".format(i 1))
  • Related