Reputation: 10904
I am trying to write a new dataframe into the CSV file. But it is not being written into the file(empty file) neither giving any error.
I feel the issue is with this line. Since I am trying to write a single value to the column.
order['trade_type'] = trade_type
Any idea what's wrong here.
def write_transaction_to_file(trade_type):
order = pd.DataFrame()
order['trade_type'] = trade_type
order.to_csv("open_pos.csv", sep='\t', index=False)
write_transaction_to_file('SELL')
Upvotes: 1
Views: 346
Reputation: 30971
Your code creates an empty DataFrame, without even column names.
And now look at order['trade_type'] = trade_type
.
If order contained some rows, among columns there were one named just 'trade_type' (string) and trade_type (variable) was a scalar, then in all rows in order would receive this value (in this column).
But since order contains no rows, there is no place to write to.
Instead you can append a row, e.g.:
order = order.append({'trade_type': trade_type}, ignore_index=True)
The rest of code is OK, the output file name as ordinary string is also OK.
Other solution: Just create a DataFrame with a single row and single named column, filled with your variable:
order = pd.DataFrame([[trade_type]], columns=['trade_type'])
Then write it to CSV file as before.
Upvotes: 2