+关注
已关注

分类  

暂无分类

标签  

暂无标签

日期归档  

暂无数据

Python - Filter dataframe based on the values

发布于2021-03-06 18:31     阅读(540)     评论(0)     点赞(21)     收藏(4)


0

1

2

3

4

5

6

7

8

9

I am performing data analysis on stocks, analyzing approx. 600,000 lines running various criteria. So far, there are maximum number of combinations it has satisfied is 20. I found a way to generate all combinations. Now next thing is to run the filter these combinations and provide the result on how many of the scenario is generating profit. The code i have is taking way longer time and so suggesting if there is anything else i can use. Below is the code

import csv
import pandas as pd
import numpy as np
import itertools


# Input Entries
dataFile = "ReferenceFile.txt" # Enter the data file with all analysis

combinationFile = "Combination.csv" # Enter the combination file, the file should be in csv file

ResultFile = "Result.csv" # Enter the result file, the file should be in csv file


#Import files
dfData = pd.read_csv(dataFile, sep = ",")

dfData.fillna("", inplace=True)

dfData["AAA"] ="AAA" # adding this column to run different set of combinations at once, combatination can be 2, 3,....10,...20 fields


i = 0
file = open(combinationFile, "r") # opening the file
file
for line in file:
    dfTemp = dfData
    line = line.replace(("\n"),"") 
    line = line.split(',')
    dfTempData = pd.DataFrame(line)
    b = ""
    for col_name in dfTempData[0]:
        z = b + "|"+ col_name
        
        
        if col_name == "AAA":
            A1=1
        else:
            dfTemp=dfTemp[dfData[col_name].str.contains(col_name)]
        
            dfTempP=dfTemp[dfData[col_name].str.contains("Profit")]
        
            a = dfTempP.shape[0]/dfTemp.shape[0]
        
            b = z
                
    #c = str(b) + "," + str(dfTemp.shape[0]) + str(dfTempP.shape[0])  
    
    
    txtfile = open(ResultFile, "a")
    txtfile.write(str(dfTemp.shape[0]) + str("|") + str(dfTempP.shape[0]) + "|" + str(b) + "\n")
    #txtfile.close()
                
    
    i = i + 1
    

txtfile = open(ResultFile, "a")
txtfile.close()
print("Complete")

Any help will be highly appreciated. Thank you!

ReferenceFile ReferenceFile is a text file separated by comma. The 2nd section of the image shows data in excel file

combinationFile enter image description here

Price greater than 4 DAY EMA, Price is lower than 4DAY EMA, Price crosses 4DAY EMA positive, Price crosses 4DAY EMA negative

Price greater than 4 DAY EMA, Price is lower than 4DAY EMA, Price crosses 4DAY EMA positive, Price crosses 4DAY EMA negative

Price greater than 4 DAY EMA, Price is lower than 4DAY EMA, Price crosses 4DAY EMA positive,AAA

Price greater than 4 DAY EMA, Price is lower than 4DAY EMA, Price crosses 4DAY EMA negative,AAA

Price greater than 4 DAY EMA, Price crosses 4DAY EMA positive, Price crosses 4DAY EMA negative,AAA Price is lower than 4DAY EMA, Price crosses 4DAY EMA positive,AAA,AAA

Price is lower than 4DAY EMA, Price crosses 4DAY EMA negative,AAA,AAA

Price crosses 4DAY EMA positive, Price crosses 4DAY EMA negative,AAA,AAA

Price greater than 4 DAY EMA, Price is lower than 4DAY EMA,AAA,AAA

Price greater than 4 DAY EMA, Price crosses 4DAY EMA positive,AAA,AAA

Price greater than 4 DAY EMA, Price crosses 4DAY EMA negative,AAA,AAA

Price greater than 4 DAY EMA,AAA,AAA,AAA

Price is lower than 4DAY EMA,AAA,AAA,AAA

Price crosses 4DAY EMA positive,AAA,AAA,AAA

Price crosses 4DAY EMA negative,AAA,AAA,AAA


解决方案


暂无回答

0

1

2

3

4

5

6

7

8



所属网站分类: 技术文章 > 问答

作者:黑洞官方问答小能手

链接: https://www.pythonheidong.com/blog/article/879590/d3cfd53fe8902c4bbb6a/

来源: python黑洞网

任何形式的转载都请注明出处,如有侵权 一经发现 必将追究其法律责任

21 0
收藏该文
已收藏

评论内容:(最多支持255个字符)