我有一个长格式的数据帧,包含以下列:date、ticker、mcap、rank\u mcap。mcap列是“marketcap”,用来衡量某只股票有多大,而mcap\u rank只是它的排名(其中1是最大的marketcap)。你知道吗
我想创建一个前十大市值加权资产(如标准普尔10)。在R我做这个
df %>%
filter(day(date) == 1, rank_mcap < 11) %>%
group_by(date) %>%
mutate(weight = mcap / sum(mcap)) %>%
ungroup() %>%
我在熊猫里做什么?我得到以下错误
AttributeError: Cannot access callable attribute 'assign' of 'DataFrameGroupBy' objects, try using the 'apply' method
当我使用类似于R方法的方法时,即在python中,请执行以下操作:
df.\
query('included == True & date.dt.day == 1'). \
groupby('date').\
assign(w=df.mcap / df.mcap.sum())
我研究了http://pandas.pydata.org/pandas-docs/stable/comparison_with_r.html,没有得出结论。你知道吗
大熊猫如何在R中实现变异
相关问题 更多 >
编程相关推荐