[英]how can i filter a dataframe by some criteria and then save .csv?
Hi guys i started at jupyter notebook few days ago.大家好,我几天前开始使用 jupyter notebook。
I need help, i have a dataframe by panda.我需要帮助,我有一个熊猫的 dataframe。 something like this
像这样的东西
Date Stock Company Volume
01/02 APPL3 Apple 1.000.000
01/02 YUSS Yusduqs 200.000
01/02 APPL4 Apple 200.000
01/02 DISN Disney 1.500.000
02/02 APPL3 Apple 100.000
02/02 YUSS Yusduqs 1.250.000
02/02 DISN Disney 2.000.000
02/02 APPL4 Apple 1.250.000
... ... ....
I need to select the stock that was traded in more than 80% of the days with volume greater than $ 500.000,00 per day.我需要 select 股票在 80% 以上的日子里交易量超过每天 500.000,00 美元。
And i need to select **only one stock per firm, the criterio is which has more volume in all days combined.我需要 select **每家公司只有一只股票,标准是在所有日子里总成交量更多。 Like for 'Apple' in [Company] i have two diferents [Stock] Appl3 and Appl4, in this specific case i only need APPL4.
就像 [Company] 中的“Apple”,我有两个不同的 [Stock] Appl3 和 Appl4,在这种特定情况下,我只需要 APPL4。
(Because Volume of the days combined in Appl4 > Volume of the days combined in Appl3) (因为 Appl4 中合并的天数 > Appl3 中合并的天数)
I started like this:我是这样开始的:
unique_dates=len(df['Date'].value_counts()) share_freq=df[df['Volume']>=500000]]['Stock'].value_counts() stocks=share_freq/unique_dates for stock,value in stocks.items(): if(value>0.8): print(stock)
So after that i can see which one has>0.8 but i still need to select only one stock per firm.所以在那之后我可以看到哪个> 0.8但我仍然需要select每个公司只有一只股票。 I dont know how to respect all the criterios and by the end filter all the dataframe by the criterios and save in.csv
我不知道如何尊重所有的标准和最终过滤所有 dataframe 的标准并保存在.csv
We can use nunique
我们可以使用
nunique
n=df.loc[df['Volume']>=500000,'Date'].groupby(df['Stock']).nunique()
uniquedate=df.Date.nunique()
n=n[n/uniquedate >0.8]
print(n.index)
Update更新
df.loc[df.Stock.isin(n.index)].to_excel('output.xlsx')
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.