Series Functions & Feature Engineering Workout
- 02:12
Practice using series function and feature engineering. Manipulate a stock data CSV file using pandas in Python, including displaying data, creating new features like 'Price Increase' and 'Return', and generating summary statistics for these new features.
Downloads
No associated resources to download.
Glossary
Feature Engineering Pandas Python seriesTranscript
Before starting this exercise first make sure that you've imported stock data dot csv into the data frame stock data. Then let's complete the following steps. First, display the first row of stock data, then create a new feature named price increase by subtracting current price from price target. Display the first row of stock data again, then create a new feature named return by dividing price. Increase by current price display the first row of stock data again, and finally, display summary statistics for the new return feature.
So first I've imported pandas, I've created my dataframe stock data, and then I'm printing out the first row of stock data with the head function, so head, and then in parentheses one. So that'll show me the first row, which gives me an idea of the features that are in my dataframe.
Next, I'm creating a new feature called price increase. So I have the name of my dataframe stock data, so in square brackets, and then in single quotes, I'm writing price, increase the name of my new series. Then I'm taking stock data, price target, the series price target, and then I'm subtracting current price. Then I'm going to print the first row using the head function to see the difference. So here, when I print that out, you can see that we've added the new series price increase.
We're doing the same thing down here with return, so stock data and then in square brackets and single quotes, the word return for that new return series title. Then stock data price increase the series price increase divided by our current price series. Then we're printing out that first row again, and you can see that we've created another new series calledreturn.
Finally, we're using the describe function to get summary statistics on our new return series.