简体   繁体   English

如何通过某些标准过滤 dataframe 然后保存。csv?

[英]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.

相关问题 我需要通过一些标准过滤 dataframe 我不知道如何 - i Need to Filter dataframe by some criteria i dont know how 如何根据 Python 中的某些条件从数据帧或过滤器中提取值? - How can I extract values from a dataframe or filter based on some criteria in Python? 如何根据某些条件从 dataframe 创建过滤条件? - How to create filter condition from dataframe based on some criteria? 如何在覆盖模式下将 pandas dataframe 保存到 csv? - How can i save a pandas dataframe to csv in overwrite mode? 如何将熊猫数据框保存到压缩的csv文件中? - how can I save a Pandas dataframe into a compressed csv file? 如何将整个数据框保存在csv文件中? - How can I save an entire dataframe in a csv file? 如何将 dataframe 转换为 csv 并将其保存以返回? - How can I convert a dataframe to csv and save it to return it? 如何获得Django .filter(something__in = some_set)查询的* UN *匹配条件? - How can I get the *UN*matched criteria for a Django .filter(something__in=some_set) query? 如何根据Python中的简单条件过滤熊猫数据框? - How do I filter a Panda dataframe based on a simple criteria in Python? 我如何消除 CSV for Pandas Dataframe 中行尾的逗号,而只有一些逗号? - How can I eliminate comma at end of lines in CSV for Pandas Dataframe when only some have them?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM