简体   繁体   中英

How to create a dummy variable for event study in python

I have 49 years, 1970-2018, with a polarity score (sentiment analysis) assigned to each year. Via a simple regression (OLS) I would like to measure the impact of an event that happened in a specific year (1986) on the polarity scores.

Example of the data:

      Polarity
1970  0.051859
1971  0.053490
1972  0.074705
1973  0.069377
1974  0.088141
1975  0.078384
1976  0.067911
1977  0.068354
1978  0.067758
1979  0.075665
1980  0.065912
1981  0.051863
1982  0.059517
1983  0.081379
1984  0.052928
1985  0.110824
1986  0.054214
1987  0.074198
1988  0.059640
1989  0.038797
1990  0.077892

Now I would like to know how I could add a dummy variable that represents an event in 1986, so that I can measure the effect on the dependent variable: Polarity.

If you have a column in your dataframe (df) called, for example, 'year', which is a record of the years each event happened, you can just use pandas in the following way:

pd.get_dummies(df,columns=['year'],drop_first=True)

"Dropfirst" makes sure you leave out one dummy which is usually required for regression.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM