使用 Pandas - 最大值、最小值、第一个、最后一个
Working with Pandas - Max, Min, First, Last
我在 python 中有一个 pandas 数据框,它是按分钟计算的股票价格。我想用它来创建一个“3 分钟”图表。为此,我需要跨数据框向后工作并按如下方式合并它。
Open 是 3 组中的第一个 "open" 值
高是 3 组中的最大值
Min 是 3 组中的最小值
close 是 las "在集合 3
中的收盘价
我需要有关正确语法的帮助:
- 创建 3 组
- 取出我需要的值并放入一个新的数据框
这是我目前拥有的,但没有用。我做错了什么。
import pandas as pd
stock = 'SPY'
api = 'https://api.iextrading.com/1.0/stock/'+stock+'/chart/1d'
df = pd.read_json(api)
df['stock'] = stock
df1= df[['stock', 'label', 'open', 'high', 'low', 'close']]
dct = {'open': 'first', 'high': 'max', 'low': 'min',
'close': 'last', 'stock': 'first', 'label': 'last'}
(df1.set_index(pd.to_datetime(df1.label, errors='coerce'))
.groupby(pd.Grouper(freq='3min'))
.agg(dct)
.reset_index(drop=True)
)
将pd.Grouper
与agg
结合使用:
dct = {'open': 'first', 'high': 'max', 'low': 'min',
'close': 'last', 'stock': 'first', 'label': 'last'}
(df.set_index(pd.to_datetime(df.label, errors='coerce'))
.groupby(pd.Grouper(freq='3min'))
.agg(dct)
.reset_index(drop=True)
)
输出:
open high low close stock label
0 283.44 283.580 283.32 283.580 SPY 09:32 AM
1 283.54 283.690 283.54 283.675 SPY 09:35 AM
2 283.66 283.795 283.48 283.545 SPY 09:38 AM
使用resample
:
df.set_index(pd.to_datetime(df['label'], errors='coerce'))\
.resample('3T')\
.agg({'stock': 'first',
'label': 'last',
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last'})\
.reset_index(drop=True)[['stock','label','open','high','low','close']]
输出:
stock label open high low close
0 SPY 09:32 AM 283.44 283.580 283.32 283.580
1 SPY 09:35 AM 283.54 283.690 283.54 283.675
2 SPY 09:38 AM 283.66 283.795 283.48 283.545
我在 python 中有一个 pandas 数据框,它是按分钟计算的股票价格。我想用它来创建一个“3 分钟”图表。为此,我需要跨数据框向后工作并按如下方式合并它。
Open 是 3 组中的第一个 "open" 值 高是 3 组中的最大值 Min 是 3 组中的最小值 close 是 las "在集合 3
中的收盘价我需要有关正确语法的帮助:
- 创建 3 组
- 取出我需要的值并放入一个新的数据框
这是我目前拥有的,但没有用。我做错了什么。
import pandas as pd
stock = 'SPY'
api = 'https://api.iextrading.com/1.0/stock/'+stock+'/chart/1d'
df = pd.read_json(api)
df['stock'] = stock
df1= df[['stock', 'label', 'open', 'high', 'low', 'close']]
dct = {'open': 'first', 'high': 'max', 'low': 'min',
'close': 'last', 'stock': 'first', 'label': 'last'}
(df1.set_index(pd.to_datetime(df1.label, errors='coerce'))
.groupby(pd.Grouper(freq='3min'))
.agg(dct)
.reset_index(drop=True)
)
将pd.Grouper
与agg
结合使用:
dct = {'open': 'first', 'high': 'max', 'low': 'min',
'close': 'last', 'stock': 'first', 'label': 'last'}
(df.set_index(pd.to_datetime(df.label, errors='coerce'))
.groupby(pd.Grouper(freq='3min'))
.agg(dct)
.reset_index(drop=True)
)
输出:
open high low close stock label
0 283.44 283.580 283.32 283.580 SPY 09:32 AM
1 283.54 283.690 283.54 283.675 SPY 09:35 AM
2 283.66 283.795 283.48 283.545 SPY 09:38 AM
使用resample
:
df.set_index(pd.to_datetime(df['label'], errors='coerce'))\
.resample('3T')\
.agg({'stock': 'first',
'label': 'last',
'open': 'first',
'high': 'max',
'low': 'min',
'close': 'last'})\
.reset_index(drop=True)[['stock','label','open','high','low','close']]
输出:
stock label open high low close
0 SPY 09:32 AM 283.44 283.580 283.32 283.580
1 SPY 09:35 AM 283.54 283.690 283.54 283.675
2 SPY 09:38 AM 283.66 283.795 283.48 283.545