Pandas is a library in Python that is commonly used for statistical analysis, data aggregation and much more. In this post, I will discuss three methods in Pandas that are very useful. I will discuss the ‘read_clipboard()’ method, the ‘query()’ method and the ‘apply()’ method.
Let’s get started!
The Read Clipboard Method
The ‘read_clipboard()’ method provides an easy way to store text copied to the clipboard in a data frame. To start, go to Yahoo Finance and search for the Dow Jones Industrial Average. Next, click on the historical data and highlight the first few rows like below:

and press "CTRL + c" . Finally we can store the copied text in a data frame by executing the following:
import pandas as pd
df = pd.read_clipboard()
print(df.head())

Let’s write this data frame to a ‘csv’ as we will continue working with this data in the subsequent examples.
df.to_csv("dow_jones.csv")
Next, let’s move on to the ‘query()’ method.
The Query Method
The ‘query()’ method provides an easy way to select data using string expressions. Let’s demonstrate the use of this method using the same data from above.
Let’s read the csv file into a data frame:
df = pd.read_csv("dow_jones.csv")
Let’s convert the date column into a datetime object:
df['Date'] = pd.to_datetime(df['Date'])
Next, let’s create month and week columns:
df['Month'] = df['Date'].dt.month
df['Week'] = df['Date'].dt.week
Let’s also relax the display limits on the rows and columns:
pd.set_option('display.max_columns', None)
pd.set_option('display.max_rows', None)
print(df.head())

Finally, let’s use the ‘query()’ method to select data based on the week:
df = df.query('Week < 10')
print(df.head())

We can also filter by month:
df = df.query('Month == 3')
print(df.head())

We can alse filter based on any of other columns for that matter.
Now let’s move on the the ‘apply()’ method.
The Apply Method
The next method we will discuss is the ‘apply()’ method. Suppose we wanted to take the square of the ‘Open’ column. To demonstrate this using ‘apply(), let’s first remove the comma from the string values in the ‘Open’ column and convert the string values into a float:
df['Open'] = df['Open'].str.replace(',', '')
df['Open'] = df['Open'].astype(float)
We can then use the ‘apply()’ method to square the values in the column:
df['Open^2'] = df['Open'].apply(lambda x:x**2)
print(df.head())

A more practical use case is using the apply method with a custom function. Suppose we want to calculate returns from the ‘Open’ and ‘Close‘ values. Let’s remove the comma from the string values in the ‘Close‘ column and convert the string values into a float:
df['Close*'] = df['Close*'].str.replace(',', '')
df['Close*'] = df['Close*'].astype(float)
Next, let’s define a function that we will use to calculate returns:
def calculate_returns(df_in):
returns = (df_in[1] - df_in[0])/(df_in[0])*100
return returns
The formula for returns being:
(df['Close*'] - df['Open'])/(df['Open'])*100
Now let’s use the ‘apply()’ method and our custom function to calculate returns which will will store in a new column:
df['returns'] = df[['Open', 'Close*']].apply(calculate_returns, axis = 1)
Let’s print the first five rows:
print(df.head())

Conclusions
To summarize, in this post we discussed three methods in pandas. We discussed the ‘read_clipboard()’ method which allows us to read text copied to the clipboard into a data frame. We also discussed the ‘query()’ method which allows us to select and filter data with string expressions. Finally, we discussed the ‘apply()’ method which allows us to transform and create new columns with relatively simple code. The code from this post is available on GitHub. Thank you for reading!