0

I need to output CSV files in a loop. Using pandas

for i in range(1,int(len(feed_file_order)/2000)+2): 
    print(i)

    final_frame = final_frame.append(feed_file_order[u:(2000*i)])

    u=u+2000
    final_frame.drop(final_frame.index, inplace=True)

    final_frame.to_csv('C:\\Users\\7005243\\Desktop\\trail_i.csv') 

I want my output csv files as trail_1, trail_2 etc.

4
  • dont understant what is your problem, to build the file name? Commented Mar 18, 2019 at 16:43
  • change 'C:\\Users\\7005243\\Desktop\\trail_i.csv' to f'C:\\Users\\7005243\\Desktop\\trail_{i}.csv', it should work. The problem was that i in your case was the character i and not the variable ii Commented Mar 18, 2019 at 16:45
  • @Tacratis f-strings are great, but they available only from Python 3.6 onwards. Commented Mar 18, 2019 at 16:47
  • Thanks for pointing this out, you are of course correct. Commented Mar 18, 2019 at 16:49

2 Answers 2

3

Just change the last line of the loop to:

final_frame.to_csv('C:\\Users\\7005243\\Desktop\\trail_{}.csv'.format(i))

The format function will take care of putting the current value of i where the {} is.

Sign up to request clarification or add additional context in comments.

Comments

1
final_frame.to_csv('C:\\Users\\7005243\\Desktop\\trail_'+str(i)+'.csv')

You are adding i inside the string which is wrong. It's a variable which should change, so isolate it.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.