What will we cover?
In this lesson we will learn how to add new columns calculated from values in other columns in our DataFrame. This is similar to calculate in Excel on data from different columns.
Then we will demonstrate some useful function when working with financial data.
Finally, we will show how to remove (or drop) columns from our DataFrame.
Step 1: Load the data.
As usual we need to load the data into our DataFrame. You can get the CSV file from here.
import pandas as pd
data = pd.read_csv("AAPL.csv", index_col=0, parse_dates=True)
It is always a good habit to inspect the data with data.head() (see the video lesson or the in the Notebook link below the video for expected output).
Step 2: Create new columns in a DataFrame (Pandas)
To create a new column in our data set simply write as follows.
data['Daily chg'] = data['Close'] - data['Open']
The above statement will create a new column named Daily chg with the difference between column Close and Open.
Similarly, you can create a column with the normalized data as follows.
data['Normalized'] = data['Close'] / data['Close'].iloc[0]
This is how easy it is to work with.
Step 3: Get min and max in DataFrame columns
To find the minimum of a column.
data['Close'].min()
This will find the minimal value of the column Close.
To find the index of the minimum value use the following.
data['Close'].argmin()
You can do similar things as the following shows.
data['Normalized'].min()
data['Normalized'].argmin()
data['Close'].max()
data['Close'].argmax()
Step 4: Get the mean value of a column in a DataFrame (Pandas)
To get the mean value of a column, simply use mean().
data['Close'].mean()
Step 5: Remove / Delete columns in a DataFrame
It is always good practice to remove the columns of data we do not intend to use anymore. This can be done by using drop().
data.drop(labels=['High', 'Low', 'Adj Close', 'Volume'], axis=1, inplace=True)
Where we use the following arguments.
- labels=[‘High’, ‘Low’, ‘Adj Close’, ‘Volume’] sets the labels of the columns we want to remove.
- axis=1 sets the axis of the labels. Default is 0, and will look for the labels on the index. While axis 1 is the column names.
- inplace=True says it should actually remove the columns on the DataFrame we work on. Otherwise it will return a new DataFrame without the columns.
Want to learn more?
This is part of the course of Master Technical Analysis with pandas.
In the next lesson you will learn Matplotlib Visualization for DataFrame Time Series Data.
12% Investment Solution
Would you like to get 12% in return of your investments?
D. A. Carter promises and shows how his simple investment strategy will deliver that in the book The 12% Solution. The book shows how to test this statement by using backtesting.
Did Carter find a strategy that will consistently beat the market?
Actually, it is not that hard to use Python to validate his calculations. But we can do better than that. If you want to work smarter than traditional investors then continue to read here.
Python for Finance: Unlock Financial Freedom and Build Your Dream Life
Discover the key to financial freedom and secure your dream life with Python for Finance!
Say goodbye to financial anxiety and embrace a future filled with confidence and success. If you’re tired of struggling to pay bills and longing for a life of leisure, it’s time to take action.
Imagine breaking free from that dead-end job and opening doors to endless opportunities. With Python for Finance, you can acquire the invaluable skill of financial analysis that will revolutionize your life.
Make informed investment decisions, unlock the secrets of business financial performance, and maximize your money like never before. Gain the knowledge sought after by companies worldwide and become an indispensable asset in today’s competitive market.
Don’t let your dreams slip away. Master Python for Finance and pave your way to a profitable and fulfilling career. Start building the future you deserve today!
Python for Finance a 21 hours course that teaches investing with Python.
Learn pandas, NumPy, Matplotlib for Financial Analysis & learn how to Automate Value Investing.
“Excellent course for anyone trying to learn coding and investing.” – Lorenzo B.
