How to write into csv using the results generated from a DataFrame in python?
<p>I am reading data from a tsv file using DataFrame from Pandas module in Python. </p>

<pre><code>df = pandas.DataFrame.from_csv(filename, sep='\t')
</code></pre>

<p>The file has around 5000 columns (4999 test parameters and 1 result / output value).</p>

<p>I iterate through the entire tsv file and check if the result value matches the value that is expected. I then write this row inside another csv file. </p>

<pre><code>expected_value = 'some_value'
with open(file_to_write, 'w') as csvfile:
  csvfwriter = csv.writer(csvfile, delimiter='\t')
  for row in df.iterrows():
    result = row['RESULT']
    if expected_value.lower() in str(result).lower():
        csvwriter.writerow(row)
</code></pre>

<p>But in the output csv file, the result is not proper, i.e. the individual column values are not going into their respective columns / cells. It is getting appended as rows. How do I write this data correctly in the csv file?</p>

<p>The answers suggested works well however, I need to check for multiple conditions. I have a list which has some values:</p>

<p>vals = ['hello', 'foo', 'bar']
One of the column for all the rows has values that looks like this 'hello,foo,bar'. I need to do two checks, one if any value in the vals list is present in the column with the values 'hello, foo, bar' or if the result value matches the expected value. I have written the following code</p>

<pre><code>df = pd.DataFrame.from_csv(filename, sep='\t')
for index, row in df.iterrows():
  csv_vals = row['COL']
  values = str(csv_vals).split(",")
  if(len(set(vals).intersection(set(values))) &gt; 0 or expected_value.lower() in str(row['RESULT_COL'].lower()):
    print row['RESULT_COL']
</code></pre>