Using Pandas, perform a series of data manipulation and analysis tasks on Indonesia banking sector data provided by Sectors.
.env
file
containing the necessary API keys.Benchmarking Current Valuation
Assessing Growth Expectations
Evaluating Market Cycles
datasets
folder. Using
what you’ve learned in the Pandas tutorial, you should know how to read this data into a DataFrame:
head()
method and the shape
attribute.pe
is greater than 15
and when symbol
is equal to BRIS.JK
. Name this new DataFrame bris_high
.pe
, pb
, and ps
values for the year 2022.median()
method after filtering the DataFrame for the year 2022.ps
in descending order, then reorder the columns
so that symbol
is the first column, year
, ps
, pe
, and pb
. Drop the company_name
column.sort_values()
method, and optionally drop()
method to remove the company_name
column.
Recall that in sort_values
, you can specify the ascending
parameter to False
to sort in descending order.
You may also choose the columns
parameter in the reindex()
method to reorder the columns.symbol
and
calculate the average pe
, pb
, and ps
values for each group. Display the results.
Now, do the same but grouped by year
and calculate the average pe
, pb
, and ps
values for each year.groupby()
method followed by the mean(numeric_only=True)
method.pandas
, the mean()
method would include all numeric columns by default.
There is now a FutureWarning
that suggests numeric_only
defaulting to False
in the future.We can either specify numeric_only=True
to avoid the warning, or use the select_dtypes()
method to filter
out only the numeric columns before calling the mean()
method.requests
library, as you have seen in your Quick start guide.
fetch_data
we need to specify a url. This can be constructed from
reading the correponding API documentation. For example, to fetch the historical P/E ratios of BBRI, one could do:
pd.json_normalize
to convert the JSON response into a DataFrame.
Here’s the output:
historical_valuation
key contains the data we’re interested in, and we can extract it using pd.json_normalize
once we’ve used .explode()
to expand out the list of dictionaries into separate rows.
pd.json_normalize
to expand the dictionary into separate columns:
json_normalize
: Converts JSON data into a DataFrameexplode
: Expands out a list of dictionaries into separate rowsbanks
list, and then concatenate the DataFrames together to form the final dataset.
Show full code