基于列值dataframa python创建值

2024-09-28 14:55:44 发布

您现在位置:Python中文网/ 问答频道 /正文

我想这就是我想问的问题。我试图根据其他列周围的某些条件向新添加的列添加值

import pandas as pd
# from datetime import timedelta

"""
read csv file
clean date column
convert date str to datetime
sort for equity options
replace date str column with datetime column
"""
trade_reader = pd.read_csv('TastyTrades.csv')
trade_reader['Date'] = trade_reader['Date'].replace({'T': ' ', '-0500': ''}, regex=True)
date_converter = pd.to_datetime(trade_reader['Date'], format="%Y-%m-%d %H:%M:%S")
options_frame = trade_reader.loc[(trade_reader['Instrument Type'] == 'Equity Option')]
clean_frame = options_frame.replace(to_replace=['Date'], value='date_converter')

# Separate opening transaction from closing transactions, combine frames
opens = clean_frame[clean_frame['Action'].isin(['BUY_TO_OPEN', 'SELL_TO_OPEN'])]
closes = clean_frame[clean_frame['Action'].isin(['BUY_TO_CLOSE', 'SELL_TO_CLOSE'])]
open_close_set = set(opens['Symbol']) & set(closes['Symbol'])
open_close_frame = clean_frame[clean_frame['Symbol'].isin(open_close_set)]

'''
convert Value to float
sort for trade readability
write
'''
ocf_float = open_close_frame['Value'].astype(float)
ocf_sorted = open_close_frame.sort_values(by=['Symbol', 'Call or Put', 'Date'], ascending=True)
# for readability, revert back to ocf_sorted below
ocf_list = ocf_sorted.drop(
    ['Type', 'Instrument Type', 'Description', 'Average Price', 'Commissions', 'Fees', 'Multiplier'], axis=1)
ocf_list['Strategy'] = ''
ocf_list.to_csv('Sorted.csv')

debit_single = []
# vertical = []
# iron_condor = []
# delta = timedelta(seconds=10)

for a in ocf_list:
    for b in ocf_list['Underlying Symbol']:
        for c in ocf_list['Symbol']:
            for d in ocf_list['Date']:
                if d.count(d) == 2:
                    debit_single.append(a)

样本:

,Date,Action,Symbol,Value,Quantity,Underlying Symbol,Expiration Date,Strike Price,Call or Put,Strategy
258,2020-02-03 15:49:46,BUY_TO_OPEN,AA    200320P00013000,-37.00,1.0,AA,3/20/20,13.0,PUT,
232,2020-02-05 09:34:42,SELL_TO_CLOSE,AA    200320P00013000,12.00,1.0,AA,3/20/20,13.0,PUT,
259,2020-02-03 15:49:46,SELL_TO_OPEN,AA    200320P00014000,72.00,1.0,AA,3/20/20,14.0,PUT,
233,2020-02-05 09:34:42,BUY_TO_CLOSE,AA    200320P00014000,-32.00,1.0,AA,3/20/20,14.0,PUT,
252,2020-02-03 15:51:01,SELL_TO_OPEN,AAL   200320C00030000,49.00,1.0,AAL,3/20/20,30.0,CALL,
99,2020-02-24 09:31:07,BUY_TO_CLOSE,AAL   200320C00030000,-28.00,1.0,AAL,3/20/20,30.0,CALL,
254,2020-02-03 15:51:01,BUY_TO_OPEN,AAL   200320C00031000,-33.00,1.0,AAL,3/20/20,31.0,CALL,
100,2020-02-24 09:31:06,SELL_TO_CLOSE,AAL   200320C00031000,20.00,1.0,AAL,3/20/20,31.0,CALL,
255,2020-02-03 15:51:01,BUY_TO_OPEN,AAL   200320P00023000,-33.00,1.0,AAL,3/20/20,23.0,PUT,
195,2020-02-12 11:58:10,SELL_TO_CLOSE,AAL   200320P00023000,9.00,1.0,AAL,3/20/20,23.0,PUT,
253,2020-02-03 15:51:01,SELL_TO_OPEN,AAL   200320P00024000,49.00,1.0,AAL,3/20/20,24.0,PUT,
197,2020-02-12 11:58:10,BUY_TO_CLOSE,AAL   200320P00024000,-13.00,1.0,AAL,3/20/20,24.0,PUT,

前四行的策略列应为“垂直”,而后8行的策略列应为“铁秃鹰”


Tags: tocleanforclosedateputbuyopen