'Python Pandas. How to extract single column from downloaded yahoo_fin option chain data?
What is the proper way to extract a single column from downloaded option_chain from yahoo_fin? My code for EXXON MOBILE option chains:
from yahoo_fin import options
import pandas as pd
from IPython.display import display, HTML
df = options.get_options_chain("XOM")
pd.set_option('display.max_columns', None)
display(df)
display_max_columns does not work either.
Anyway I tried doing some numerical and name extractions, but it did not work out at all. any ideas?
Solution 1:[1]
The results are stored in a dict with key-value values of the type of option and the corresponding pd.DataFrame. You can get the keys of the dict using keys(). Keys are 'calls' or 'puts'.
from yahoo_fin import options
import pandas as pd
data = options.get_options_chain("XOM")
# access dict by key
calls = data['calls']
puts = data['puts']
calls
Output:
Contract Name Last Trade Date Strike Last Price Bid Ask Change % Change Volume Open Interest Implied Volatility
0 XOM220429C00045000 2022-04-21 11:13AM EDT 45.0 43.95 0.00 0.0 0.0 - - 0 0.00%
1 XOM220429C00060000 2022-03-28 10:19AM EDT 60.0 22.45 0.00 0.0 0.0 - 1 0 0.00%
2 XOM220429C00065000 2022-04-13 2:37PM EDT 65.0 21.40 0.00 0.0 0.0 - 4 0 0.00%
3 XOM220429C00070000 2022-04-21 11:05AM EDT 70.0 19.22 0.00 0.0 0.0 - 50 0 0.00%
4 XOM220429C00072000 2022-04-19 2:41PM EDT 72.0 15.91 0.00 0.0 0.0 - 2 0 0.00%
To access a single column of the DataFrame use the column name as a key e. g., 'Contract Name'.
calls['Contract Name']
Output:
0 XOM220429C00045000
1 XOM220429C00060000
2 XOM220429C00065000
3 XOM220429C00070000
4 XOM220429C00072000
5 XOM220429C00073000
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | KarelZe |
