I am reading data from a tsv file using DataFrame from Pandas module in Python.
df = pandas.DataFrame.from_csv(filename, sep='\t')
The file has around 5000 columns (4999 test parameters and 1 result / output value).
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.
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)
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?
The answers suggested works well however, I need to check for multiple conditions. I have a list which has some values:
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
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))) > 0 or expected_value.lower() in str(row['RESULT_COL'].lower()):
print row['RESULT_COL']
df.to_csv(file_to_write)?