[英]Creating a list of lists from a dataframe based on a condition
im working with some financial data and I want to create a list of lists while iterating through a df and a certain condition is met我正在处理一些财务数据,我想在遍历 df 并满足某个条件时创建一个列表列表
eg: df例如:df
25 Day 250 Day
Date
2001-12-07 1.4 1.5
2001-12-10 1.6 1.7
2001-12-11 1.8 1.2
2001-12-12 1.4 1.5
2001-12-13 1.4 1.6
I want to loop through the rows and make a list when 25 Day < 250 Day.我想在 25 天 < 250 天时遍历行并列出一个列表。 This condition is intermittent and may last for a couple rows at a time before breaking.
这种情况是间歇性的,可能会在中断前一次持续几行。 I want to eventually use this data to plot something on a graph.
我想最终将这些数据用于 plot 图表上的某些东西。
In the example above I should get the following: [[2001-12-07, 2001-12-10], [2001-12-12,2001-12-13]]在上面的示例中,我应该得到以下信息: [[2001-12-07, 2001-12-10], [2001-12-12,2001-12-13]]
Im currently using the following to iterate through the rows and get a list of all of the values, but don't know how I would put all of the continuous values inside separate lists我目前使用以下内容遍历行并获取所有值的列表,但不知道如何将所有连续值放入单独的列表中
def get_Date():
global date_Buy
x = []
date_Buy = [[x]]
for i, value in master_Data.iterrows():
if value['25 Day'] < value['250 Day']:
x.append(i)
else:
continue
date_Buy.append(x)
get_Date()
Essentially I would like to start a new list each time the continue is triggered基本上我想在每次触发 continue 时开始一个新列表
Do you want something like this?你想要这样的东西吗?
import pandas as pd
df = pd.DataFrame({'Date': ['2018-01-02', '2019-03-31', '2019-03-31', '2018-01-02', '2018-01-02', '2019-04-31',
'2018-01-02'],
'Day_25': [1, 920, 920, 920, 921, 921, 921],
'Day_250': [100, 2, 3000, 4, 600, 7, 8]})
index_greater=df[df.Day_25 > df.Day_250].index
You can filter using query
then convert each row to list using agg
:您可以使用
query
进行过滤,然后使用agg
将每一行转换为列表:
lst = df.query("Day_25 > Day_250").reset_index().agg(list, 1).tolist()
print(lst)
[['2019-03-31', 920, 2],
['2018-01-02', 920, 4],
['2018-01-02', 921, 600],
['2019-04-31', 921, 7],
['2018-01-02', 921, 8]]
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.