jmbejara / comp-econ-sp19

Main Course Repository for Computational Methods in Economics (Econ 21410, Spring 2019)
48 stars 26 forks source link

HW 3 Indexing on Multiindex Dataframe #29

Closed graceslu closed 5 years ago

graceslu commented 5 years ago

For Q8, I can't figure out how to group the dataframe by firm since 'firm' is an index name, not a column in the dataframe. I added a new column 'firm' to the dataframe that matches the firm for each data point. This was enough to run a fixed effects model, but I'm wondering if there is a much better way to approach this?

jmbejara commented 5 years ago

Hi @graceslu . The groupby method includes a keyword argument called level that will allow you to group by levels in a MultiIndex. This should allow you to group by your index. See the documentation here: https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.groupby.html

However, you should be able to complete this HW without using groupby. I'm using a "for-loop" and the linearmodels module. The expression to run the fixed effects regression is this:

linearmodels.PanelOLS.from_formula('y ~ k + l + EntityEffects', data=data)