Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 使用不同列中的条件语句进行分组_Python_Python 3.x_Pandas - Fatal编程技术网

Python 使用不同列中的条件语句进行分组

Python 使用不同列中的条件语句进行分组,python,python-3.x,pandas,Python,Python 3.x,Pandas,我需要根据以下标准对pandas数据帧进行分组,这类似于ohlc聚合: open = last where volume > 0, in case there is no entry with volume > 0 use overall last high = max low = min last = last volume = max 我目前对这类操作(ohlc聚合)的实现是: 我怎样才能解决这个问题?多谢各位 示例: fi ts

我需要根据以下标准对pandas数据帧进行分组,这类似于ohlc聚合:

open = last where volume > 0, in case there is no entry with volume > 0 use overall last
high = max
low = min
last = last
volume = max
我目前对这类操作(ohlc聚合)的实现是:

我怎样才能解决这个问题?多谢各位

示例:

                     fi  ts     open     high      low    close  volume
datetime                                                               
2017-11-17 12:35:00   0   0  0.96214  0.96214  0.96214  0.96214       0
2017-11-17 12:35:00   0   0  0.96214  0.96214  0.96214  0.96214       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96213  0.96220  0.96213  0.96219      19
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96219       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96222       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96223       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96220      29
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96220      29
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96222       0
                     fi  ts     open     high      low    close  volume
datetime 
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96222       29
所需输出:

                     fi  ts     open     high      low    close  volume
datetime                                                               
2017-11-17 12:35:00   0   0  0.96214  0.96214  0.96214  0.96214       0
2017-11-17 12:35:00   0   0  0.96214  0.96214  0.96214  0.96214       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96213  0.96220  0.96213  0.96219      19
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96219       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96222       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96223       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96220      29
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96220      29
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96222       0
                     fi  ts     open     high      low    close  volume
datetime 
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96222       29
其他信息:

                     fi  ts     open     high      low    close  volume
datetime                                                               
2017-11-17 12:35:00   0   0  0.96214  0.96214  0.96214  0.96214       0
2017-11-17 12:35:00   0   0  0.96214  0.96214  0.96214  0.96214       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96213  0.96220  0.96213  0.96219      19
2017-11-17 12:35:00   0   0  0.96214  0.96220  0.96214  0.96219       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96222       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96222  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96223       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96220       0
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96220      29
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96220      29
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96221       0
2017-11-17 12:35:00   0   0  0.96214  0.96223  0.96214  0.96222       0
                     fi  ts     open     high      low    close  volume
datetime 
2017-11-17 12:35:00   0   0  0.96213  0.96223  0.96213  0.96222       29
有两个数据源可通过其“体积”值识别:

a. Volume = 0 (more frequent, less reliable)
b. Volume > 0 (less frequent, more reliable)
由于类型“b”更可靠,因此最好使用其打开值来输入“a”打开值

至于最后一次聚合是否成功,老实说并不重要,其他聚合(first、max、min)也可以,因为open值是一分钟内第一个引用的值(在本例中),并且永远不会更改


当与服务器的连接中断时,会出现错误值的问题。类型“a”数据无法处理此问题,可能会给我错误的值,类型“b”数据可以很好地处理此问题,并会给我正确的值。

您可以使用
布尔索引
获取最大容量,使用
尾部(1)
获取打开内部应用的最后一个值,因为您有重复的索引,即

ohlc_dict = {
   'high': 'max',
   'low': 'min',
   'close': 'last',
   'volume': 'max',
}
grp = df.groupby(pd.Grouper(freq='1Min',level=0,label='left'))
ndf = grp.agg(ohlc_dict)

ndf['open'] = grp['open','volume'].apply(lambda x : x[x['volume'] == x['volume'].max()].tail(1)['open'])
输出:

low volume close high open datetime 2017-11-17 12:35:00 0.96213 29 0.96222 0.96223 0.96213 低音量关闭高音量打开 日期时间 2017-11-17 12:35:00 0.96213 29 0.96222 0.96223 0.96213
您可以使用
布尔索引
获取最大容量,使用
尾部(1)
获取打开内部应用的最后一个值,因为您有重复的索引,即

ohlc_dict = {
   'high': 'max',
   'low': 'min',
   'close': 'last',
   'volume': 'max',
}
grp = df.groupby(pd.Grouper(freq='1Min',level=0,label='left'))
ndf = grp.agg(ohlc_dict)

ndf['open'] = grp['open','volume'].apply(lambda x : x[x['volume'] == x['volume'].max()].tail(1)['open'])
输出:

low volume close high open datetime 2017-11-17 12:35:00 0.96213 29 0.96222 0.96223 0.96213 低音量关闭高音量打开 日期时间 2017-11-17 12:35:00 0.96213 29 0.96222 0.96223 0.96213
您可以首先按
打开
列的
最后一个
进行聚合:

ohlc_dict = {
   'high': 'max',
   'low': 'min',
   'close': 'last',
   'open':'last',
   'volume':'sum'
}

g = df.groupby(pd.Grouper(freq='1Min',level=0,label='left'))
df2 = g.agg(ohlc_dict)
print (df2)
                         low    close     high     open  volume
datetime                                                       
2017-11-17 12:35:00  0.96213  0.96222  0.96223  0.96215      77
然后过滤掉所有
0
卷,并仅聚合
open
的最后一个值:

g1 = df[df['volume'] > 0].groupby(pd.Grouper(freq='1Min',level=0,label='left'))
df1 = g1['open'].last().reindex(df2.index)
print (df1)
datetime
2017-11-17 12:35:00    0.96213
Freq: T, Name: open, dtype: float64
最后使用和将两个数据帧合并为一个:


使用带条件的自定义函数(slowier):


您可以首先按
打开
列的
最后一个
进行聚合:

ohlc_dict = {
   'high': 'max',
   'low': 'min',
   'close': 'last',
   'open':'last',
   'volume':'sum'
}

g = df.groupby(pd.Grouper(freq='1Min',level=0,label='left'))
df2 = g.agg(ohlc_dict)
print (df2)
                         low    close     high     open  volume
datetime                                                       
2017-11-17 12:35:00  0.96213  0.96222  0.96223  0.96215      77
然后过滤掉所有
0
卷,并仅聚合
open
的最后一个值:

g1 = df[df['volume'] > 0].groupby(pd.Grouper(freq='1Min',level=0,label='left'))
df1 = g1['open'].last().reindex(df2.index)
print (df1)
datetime
2017-11-17 12:35:00    0.96213
Freq: T, Name: open, dtype: float64
最后使用和将两个数据帧合并为一个:


使用带条件的自定义函数(slowier):


你能添加一些样本吗?@Jezrael,卷有两类:a。体积>0,b。体积=0。(这些类别代表不同的数据源)。添加所需的输出。谢谢。好的,对不起,我解释得不好。首先感谢您提供的示例,然后您是否可以进一步解释
open
aggregation-
last where volume>0,如果没有volume>0的条目,请使用总体last
?如果可能的话,最好为
volumn<0
添加另一列,我再次阅读了它,似乎我理解了-需要最后一个非0值,对于它
open
@Jezrael,完美。但是,有时不会出现非0值。我也在原始帖子上添加了一些信息。你能添加一些示例吗?@Jezrael,卷有两个类别:a。体积>0,b。体积=0。(这些类别代表不同的数据源)。添加所需的输出。谢谢。好的,对不起,我解释得不好。首先感谢您提供的示例,然后您是否可以进一步解释
open
aggregation-
last where volume>0,如果没有volume>0的条目,请使用总体last
?如果可能的话,最好为
volumn<0
添加另一列,我再次阅读了它,似乎我理解了-需要最后一个非0值,对于它
open
@Jezrael,完美。但是,有时不会出现非0值。我也在原来的帖子上添加了一些信息。