我尝试计算来自不同公司/代码的一些KPI。我的stock-info位于df中,具有此结构
Ticker Open High Low Adj Close Volume
Date
2015-04-09 vws.co 315.000000 316.100000 312.500000 311.520000 1686800
2015-04-10 vws.co 317.000000 319.700000 316.400000 312.700000 1396500
2015-04-13 vws.co 317.900000 321.500000 315.200000 315.850000 1564500
2015-04-14 vws.co 320.000000 322.400000 318.700000 314.870000 1370600
2015-04-15 vws.co 320.000000 321.500000 319.200000 316.150000 945000
2015-04-16 vws.co 319.000000 320.200000 310.400000 307.870000 2236100
2015-04-17 vws.co 309.900000 310.000000 302.500000 299.100000 2711900
2015-04-20 vws.co 303.000000 312.000000 303.000000 306.490000 1629700
... ... ... ... ... ... ...
2016-03-31 mmm 166.750000 167.500000 166.500000 166.630005 1762800
2016-04-01 mmm 165.630005 167.740005 164.789993 167.529999 1993700
2016-04-04 mmm 167.110001 167.490005 165.919998 166.399994 2022800
2016-04-05 mmm 165.179993 166.550003 164.649994 165.809998 1610300
2016-04-06 mmm 165.339996 167.080002 164.839996 166.809998 2092200
2016-04-07 mmm 165.880005 167.229996 165.250000 167.160004 2721900
我需要在每个自动收报机的基础上进行计算,所以在遍历公司/代码时我不会扭曲移动平均结果。
有人可以帮助我吗?
拆分和连接dfs 我如何逻辑地将代码分离成可能附加有自动收录器名称的不同dfs,然后如何将它们全部附加到单个df?
从同一df中选择 或者只选择相同df中的相关股票行? (可能就像这个Splitting dataframe into multiple dataframes)
感谢任何帮助...
----------来自Alexanders的帖子如下。 这段代码
df['MA1'] = df.groupby('Ticker').df['Adj Close'].transform(lambda group: pd.rolling_mean(group, window=10))
引发此错误:
AttributeError: 'DataFrameGroupBy' object has no attribute 'df'
或更贴心的粘贴
Adj_Close = df['Adj Close']
df['MA3'] = df.groupby('Ticker').Adj_Close.transform(lambda group: pd.rolling_mean(group, window=3))
不再抛出错误。这是col' Adj Close'有一个groupby不喜欢的空间......这一定是个bug
如果我尝试这种语法,那应该有用 - 它没有!
df['MA3'] = df.groupby('Ticker').df["Adj Close"].transform(lambda group: pd.rolling_mean(group, window=3))
并抛出此错误:
AttributeError: 'DataFrameGroupBy' object has no attribute 'df'
但我可以使用上述内容。谢谢亚历山大
答案 0 :(得分:1)
您可以在str3
对象上使用transform
来维护具有相同形状的列:
例如,这里是Adj Close的3天移动平均值(Pandas <0.18.0)。
groupby
答案 1 :(得分:0)
使用groupby
print df
Ticker Open High Low Adj_Close Volume
Date
2015-04-09 vws.co 315.000000 316.100000 312.500000 311.520000 1686800
2015-04-10 vws.co 317.000000 319.700000 316.400000 312.700000 1396500
2015-04-13 vws.co 317.900000 321.500000 315.200000 315.850000 1564500
2015-04-14 vws.co 320.000000 322.400000 318.700000 314.870000 1370600
2015-04-15 vws.co 320.000000 321.500000 319.200000 316.150000 945000
2015-04-16 vws.co 319.000000 320.200000 310.400000 307.870000 2236100
2015-04-17 vws.co 309.900000 310.000000 302.500000 299.100000 2711900
2015-04-20 vws.co 303.000000 312.000000 303.000000 306.490000 1629700
2016-03-31 mmm 166.750000 167.500000 166.500000 166.630005 1762800
2016-04-01 mmm 165.630005 167.740005 164.789993 167.529999 1993700
2016-04-04 mmm 167.110001 167.490005 165.919998 166.399994 2022800
2016-04-05 mmm 165.179993 166.550003 164.649994 165.809998 1610300
2016-04-06 mmm 165.339996 167.080002 164.839996 166.809998 2092200
2016-04-07 mmm 165.880005 167.229996 165.250000 167.160004 2721900
看起来像:
df.groupby('Ticker').sum()
Open High Low Adj_Close Volume
Ticker
mmm 995.89 1003.590011 991.949981 1000.339998 12203700
vws.co 2521.80 2543.400000 2497.900000 2484.550000 13541100
groupby
您可以使用if url.endswith("html"):
yield scrapy.Request(url, meta={"item": item}, callback=self.parse_details)
else:
yield item
if url2.endswith("html"):
yield scrapy.Request(url2, meta={"item": item}, callback=self.parse_details2)
else:
yield item
对象聚合并执行许多操作。