1

I am new to Python. How can I write to a CSV file while iterating through a DataFrame?

readCSV1 = pd.read_csv(r'ProductDetails1.csv')
df1 = DataFrame(readCSV1)

for index, row in df1.iterrows():
print(index, row['SKU'], row['SKU'],row['Title'],row['Cost'],row['Weight 
    Value'],row['Weight Unit']...............)

I am iterating through a large csv file that contains many columns, however I would like to be able to limit the columns in the final csv file as well as put in other arguments. The print output to the console is correct, however I am having trouble figuring out how best to print this to a new csv file as I will be performing many other functions and do not want to simply edit the dataframe itself.

3
  • 2
    You should not write to the csv file line by line. It is highly inefficient. You should instead clean / limit your dataframe to the contents you want to write, and then call pd.DataFrame.to_csv() all at once: pandas.pydata.org/pandas-docs/stable/generated/… Commented Oct 12, 2018 at 18:52
  • The problem is that I need to iterate through each line as certain variables could affect the item below it. Commented Oct 12, 2018 at 18:54
  • Then you should perform those operations on the dataframe once you have read it in, then write back once. If you are having issues with specific parts of that logic, then you should update your question to reflect those. Commented Oct 12, 2018 at 18:55

2 Answers 2

2

You should not write to the csv file line by line. It is highly inefficient. You should instead clean / limit your dataframe to the contents you want to write, and then call pd.DataFrame.to_csv() all at once.

For example:

df = pd.read_csv(r'ProductDetails1.csv')

#Refine your dataframe here
refined_df = df[['SKU','Title','Close','Weight Value','Weight Unit']]

refined_df.to_csv(r'ProductDetails1.csv', index=False)
Sign up to request clarification or add additional context in comments.

Comments

1

I would like to be able to limit the columns in the final csv file as well as put in other arguments.

Don't make these adjustments as you write your CSV file. Make the adjustments in the dataframe itself before you export to CSV. Then use the purpose-built pd.DataFrame.to_csv method.

For example, you can select specific columns in a specific order before exporting:

df = df[['SKU', 'SKU', 'Title', 'Cost']]
df.to_csv('file.csv', index=False)

To "put in other arguments", make new columns via appropriate logic.

2 Comments

I would also need to implement conditional logic to determine the contents of one of my columns, how best do I implement this in a data frame?
@TonaldDrump, I really can't answer on such limited information. I suggest you read a Pandas tutorial; and, if you're still stuck and can't find an answer on SO, ask a new question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.