Reputation: 90
Most answer given to the questions that seemed like the answers would help weren't helpful to anyone asking any questions. Nor the people answering the questions knew figure things out after they found out what they had contributed didn't work. I have tried pretty much every str() and .to_string variation I could find.
Anyways, I've been trying to get data in a file paired up and omit data I can't pair up. I believe I've paired things together, but there's no way for me to verify this other than seeing the column and true or false.
import pandas as pd
# read file
with open('TastyTrades.csv', 'r') as trade_history:
trade_reader = pd.read_csv('TastyTrades.csv')
# sort out for options only
options_frame = trade_reader.loc[(trade_reader['Instrument Type'] == 'Equity Option')]
# resort data
date_frame = options_frame.sort_values(by=['Symbol', 'Date', 'Action'], ascending=True)
# pair BTO to STC
BTO = date_frame['Action'].isin(['BUY_TO_OPEN', 'SELL_TO_CLOSE'])
STO = date_frame['Action'].isin(['SELL_TO_OPEN', 'BUY_TO_CLOSE'])
# bringing both frames as one
pairs = [BTO, STO]
# return readable data
result = pd.concat(pairs).astype(str)
# write to new file
result.to_csv('new_taste.csv')
This code bring me:
,Action
101,True
75,True
102,False
76,False
95,False
97,True
98,True
38,True
174,True
166,True
I am trying to get the data back to the readable format:
Date,Type,Action,Symbol,Instrument Type,Description,Value,Quantity,Average Price,Commissions,Fees,Multiplier,Underlying Symbol,Expiration Date,Strike Price,Call or Put
2020-02-14T15:49:12-0500,Trade,SELL_TO_OPEN,TGT 200327C00127000,Equity Option,Sold 1 TGT 03/27/20 Call 127.00 @ 1.33,133,1,133,-1,-0.15,100,TGT,3/27/2020,127,CALL
2020-02-14T15:49:11-0500,Trade,SELL_TO_OPEN,TGT 200327P00107000,Equity Option,Sold 1 TGT 03/27/20 Put 107.00 @ 1.80,180,1,180,-1,-0.15,100,TGT,3/27/2020,107,PUT
2020-02-14T15:49:11-0500,Trade,BUY_TO_OPEN,TGT 200327C00128000,Equity Option,Bought 1 TGT 03/27/20 Call 128.00 @ 1.17,-117,1,-117,-1,-0.14,100,TGT,3/27/2020,128,CALL
Upvotes: 0
Views: 243
Reputation: 143
Here BTO and STO will only have the result of isin condition (true or false). So, Rewrite two of your lines as below:
BTO = date_frame[date_frame['Action'].isin(['BUY_TO_OPEN', 'SELL_TO_CLOSE'])]
STO = date_frame[date_frame['Action'].isin(['SELL_TO_OPEN', 'BUY_TO_CLOSE'])]
This will give all the columns into BTO and STO and then you can merge these two DF's. Hope this helps. Check below working code: I tried it, and got expected result. All the rows using above code. I tried without converting to 'str', that too gave me same result. Try printing the result and see what it shows.
BTO = quotes[quotes['Action'].isin(['BTO', 'STC'])]
STO = quotes[quotes['Action'].isin(['STO', 'BTC'])]
frames = [BTO,STO]
result = pd.concat(frames).astype(str)
result.to_csv('new_taste.csv')
Upvotes: 1